I am assuming you all guys have basic idea about oops and java, as all my lessons would be targeted upon nailing the OCJP 6 certification.
IS-A Relationship :
- This refers to inheritance or implementation.
- Expressed using keyword “extends”.
- Main advantage is code reusability.
Lets look an example to understand about inheritance better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | class P { public void m1() {} } class C extends P { public void m2() {} } class Test { public static void main (String [] args) { //case 1 P p = new P(); p.m1(); // compiles fine p.m2(); // Will get a compile time error as "Cannot find m2()" //case 2 C c = new C(); c.m1(); // compiles fine c.m2(); // compiles fine //case 3 P p = new C(); p.m1(); // compiles fine p.m2(); // Will get a compile time error as "Cannot find m2()" //case 4 C c = new P(); // Compiler error as "incompatible types" } } |
OBSERVATIONS from above example:
- Whatever the parent class has, is by default available to the child. Hence by using child reference, we can call both parent and child class methods.
- What ever the child class has, by default is not available to parent, hence on the parent class reference we can only call parent class methods but not child specific methods.
- Parent class reference can be used to hold child class objects , but by using that reference we can call only parent class methods but not child specific methods.
- We cant use child class reference to hold parent class objects
Entire java API is implemented based on inheritance. Every java class extends from Object class which has most common and basic methods required for all java classes. Hence we can say “Object ” class is root class of all java methods.
A point to remember on inheritance .. A java class cannot extend more than one class at a time so it wont provide support for multiple inheritance in classes, but it can extend more than one interface at a time so we can say java provides support for multiple inheritance w.r.t. interfaces.
HAS-A Relationship
- Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.
- It is also known as “composition” or “aggregation”.
- There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword.
Composition :
- Without existence of container object, if there is no chance of existence of contained objects then container and contained objects are said to be strongly associated and this strong association is known as composition.
Eg: A “university” has several “departments”. Without existence of “university” there is no chance for the “departments” to exist. Hence “university” and “departments” are strongly associated and this strong association is known as composition.
Aggregation
- Without existence of container object, if there is a chance of existence of contained objects then container and contained objects are said to be loosely associated and this strong association is known as aggregation.
Eg: A “department” has several “professors”. Without existence of “departments” there is good chance for the “professors” to exist. Hence “professors” and “department” are loosely associated and this loose association is known as Aggregation.
Thats the end of the my first lesson. I know that’s not much but from now on wards i will first give you an example and then explain topics based on that example
Next lesson “Overloading” vs “Overriding”.
Please do comment if you have any doubts about this lesson.
EXCELLENT WORK OUT HARSHA… 🙂
VERY USEFUL EXAMPLE GIVEN IN IS-A EXPLANATION… 🙂
[…] IS-A, HAS-A Relationship […]
ur 3rd point is wrong sir.
if you are saying inheritance is for reusability.
It is correct deepak, Inheritance can be used for re-usability
It is used for Reusibility
Good Explanation sir..!
Very nice work Harsha! It was fantastic……..!
gooooooooood sir………….
Please check this
P p = new C();
p.m1(); // compiles fine
p.m2(); // Will get a compile time error as “Cannot find m2()”
Just give a try p.m2 will work
diff. between class and interface…???
what is the use polymorphism in programme..??
actually
P p = new C();
p.m1(); // compiles fine
p.m2(); // It is working compiles fine
article is very nice. thank you.
thnx for understanding is-a relation ship
thanx for help
Sir is correct. I tried to compile following code.
package com.test.corejava;
class P
{
public void m1() {}
}
class C extends P
{
public void m2() {}
}
class Test
{
public static void main (String [] args)
{
//case 1
P p = new P();
p.m1(); // compiles fine
p.m2(); // Will get a compile time error as “Cannot find m2()”
//case 2
C c = new C();
c.m1(); // compiles fine
c.m2(); // compiles fine
//case 3
P p2 = new C();
p2.m1(); // compiles fine
p2.m2(); // Will get a compile time error as “Cannot find m2()”
//case 4
C c1 = new P(); // Compiler error as “incompatible types”
}
}
Yes, the code
p2.m2(); //compilation error
will give compilation error.
Reason is: Using super class reference we can call only those methods whose signature is available in both sub class as well as super class. Here Super class P does not have signature of m2().
I have a doubt. In the following code, is the statement A HAS-A B true even though we have not instantiated B?
class B{ }
public class A{
B b;
}
It become true ONLY if we instantiated B
class B{ }
public class A{
B b=new B();
}
can any one tell what is the relationship exist between sevicecentres and vehicles
Harsh Its great work and very helful for us
its very useful to known what is is-a relation and what is has-a relation and Thanks you Harsh……….
Hi .. Great Article. Might I add a few pointers:
Although code re usability maybe a reason for inheritance, I think the prime need for interfaces / abstract classes is generalization. Even when you look through the java code all interfaces that you see are generalized operations that should be supported by child classes. SO i think its fair to say that Is- a relationship is tending more towards generalization.
Regarding Composition and aggregation. The explanation is very correct. Just wanted to add the java code for the same.
//Composition relationship
class University{
private Department departments;
University(){
//Creating a university object depends on creating department objects.
//This indicates university is composed of depratments
departments = new Department()
}
}
//Aggregation relationship
class Department{
//Department consists of persons
private Person p;
//Methods which use the person Obj.
}
all your examples are straightaway from Durga sir classes
simple and cute defination.,.. thanks
[…] More info (http://learnwithharsha.com/my-first-java-lesson-is-a-and-has-a-relationships/) […]
Thank you, its useful
what is container object and contained objecf sir?
Thanks a million, your example cleared my confusion forever !!!!!
If you want to use that m2() method using parent reference then you need to implement downcasting.
Code is:
C c = new C();
P ref;
ref=c;
(C)ref.m2(); //Compiles fine without any error
Can u tell me the which relation (IS-A or HAS-A)is good or recommended to use…
why. it is recommended to use…? explain me
sir understood above code which u explained….
but please give me brief explanation about instanceOF key word.. which is used to identify is a relation
Very good explanation
Good work Harsha…
want more posts from u, u are very good in explaining….