Page 457 - Cs_withBlue_J_C11_Flipbook
P. 457
15.3.1 Access specifiers
The visibility range of the access specifiers in a package is given below.
Access Specifiers Same class Same package Subclass in the Outside package
outside package
public Yes Yes Yes Yes
protected Yes Yes Yes No
default Yes Yes No No
private Yes No No No
Let us do some programs on package.
Program 1 Write a program to create a package Eleven containing class Distance having the following
specifications.
Package : Eleven
Class name : Distance
Data Members
double x1, y1, x2, y2 : Co-ordinates of two points
Member Methods
Diatance(double x11, double y11, : Constructor to assign the data members
double x21, double y21)
double calDistance() : Calculates and returns the distance between two
points using formula
√(x2-x1) + (y2-y1) 2
2
Write a second class Circle to import Distance and calculate the radius of a circle by method
calDistance(), where the two-point co-ordinates are given. Finally, print the area of the circle
2
as A=πr in function areaCircle().
1 package Eleven; // package declaration
2 public class Distance
3 {
4 double x1,x2,y1,y2;
5 public Distance(double x11,double y11,double x21,double y21)
6 { x1=x11;
7 y1=y11;
8 x2=x21;
9 y2=y21;
10 }
11
12 public double calDistance()
13 { return Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
14 }
15 }
455
Packages 455

