Relationships in Java:
In
order to make use of the data member and methods of one class into another, in
java we have 3 types of relationships.
(1) Is –a
(2) Has-a
(3) Uses-a
(1)
is-a relationship:
Class
A
{
..…………….
………………
}
Class
B extends class A //is-a
relationship
{
..…………….
………………
}
Is-a
relationship is one in which one class is taking the feature of another class
by using the concept of inheritance with the keyword “extends”.
(2)
has-a Relationship:
Has-a
relationship is one in which an object of one class is created or declared as a
data member in another class.
Class
B
{
int a,b;
A
obj; //has-a relationship
A=10;
Obj = new A();
}
(3)
Uses-a Relationship:
Uses-a
relationship is one in which a method of one class is making use of an object
of another class.
Class
B
{
Void add ()
{
A obj = new A(); //uses-a
relationship
}
}
Note:
1) Default relationship in java is “is-a” relationship.
Since every class is taking features from java.lang.object class.
2) System.out is the default relationship belongs to
“has-a” relationship.
3) Every execution logic class is making use of an object
of business logic class, this belongs to “uses-a” relationship.
Example
of “uses-a” relationship:
Class
A
{
float calculateInterest (float p, float r, int t)
{
float si =(P*r*t)/100;
return (si);
}
}
Class
B
{
void calculation()
{
System.out.println(“enter principal”);
read (p);
System.out.println(“enter rate”);
read (r);
System.out.println(“enter
time”);
read (t);
A obj= new A();
Float var =obj.calculateInterest(….);
}
}
No comments:
Post a Comment