Header Ads

What Is IS-A Relationship And What Are The Rules Of IS-A Relationship In Java

In java Inheritance concept came to reduce the number of code line i.e code redundancy.

Say in an college we have some student,lecturer,principal.

Now for student we can have n column, so the corresponding class design will be..
class student{
int id
String name;
int age;
int No_of_Subject;
----
----
student(int id,string name,int age,......)
{
//constructor to initialize the value
this.id=id;
this.name=name;
this.age=age;
----
----

}
}
Similarly for lecturer class we will have...
class lecturer{
int id
String name;
int age;

----
----
lecturer(int id,string name,int age,......)
{
//constructor to initialize the value
this.id=id;
this.name=name;
this.age=age;
----
----

}
}
for principal we will have--
class principal{
int id
String name;
int age;

----
----
principal(int id,string name,int age,......)
{
//constructor to initialize the value
this.id=id;
this.name=name;
this.age=age;
----
----

}

}

now if you observe closely most of the code is redundant in nature. To get reduce redundancy we can create a common class and can derive more special classes from it.


How to design the common class?
It is interesting..we can create a parent class with all the common attribute..the common class is more abstract in nature.And the child classes are more specific class.Child classes can override the methods it got from parent class. It can also add more instance variable and child specific method. Instance variables are not overridden because they do not define behavior.So a sub class can give an inherited instance variable any value
like-
class College_person{
int id
String name;
int age;

----
----
College_person(int id,string name,int age,......)
{
//constructor to initialize the value
this.id=id;
this.name=name;
this.age=age;
----
----

}

}

Now the corresponding classes will become:
Student:
class student extends College_person {
int id
String name;
int age;

----
----
student(int id,string name,int age,......)
{
//constructor to initialize the value
super(id,name,age,......)
this.xyx=xyz;
----
----

}

}
Similarly for lecturer class we will have...
class lecturer extends College_person{
int id
String name;
int age;

----
----
lecturer(int id,string name,int age,......)
{
//constructor to initialize the value
super(id,name,age,......)
this.abc=abc;
----
----

}
}
for principal we will have--
class principal extends College_person{
int id
String name;
int age;

----
----
principal(int id,string name,int age,......)
{
//constructor to initialize the value
super(id,name,age,......)
this.tgb=tgb;

----
----

}

}
If you observe closely the super method is taking care of default value's initialization and specific values specific to the class are mentioned in the corresponding class.This is inheritance in nature.
the College_person becomes parent for all class student,lecturer and principal.

Cool isn't it???
But to perform a valid parent child relationship , the relationship has to go through a IS-A test. If it passes then only we can confirm the relationship is perfect.

here is student IS-A College_person? the answer is yes...hence College_person and student are in valid parent child relationship.Similarly it is yes for lecturer and principal

So the conclusions are--
  • IS-A relationship is valid inheritance
  • By using extends keyword we can implement IS-A relationship
  • It reduces the code redundancy but increases code re usability.
Let us see some one more example with respect to inheritance:
Class Parent:
Class Parent{
metho1()
{
}
}

Class Child:

Class Child extends Parent{
method2()
{
}

Class test:

Class test{
public static void main(String... Args)
{
//case-1
Parent p=new Parent();
p.method1();
p.method2();

//case-2
Child c=new Child();
c.method1();
c.method2();

//case-3
Parent p=new Child()
p.method1();
p.method2();

//case-4
Child c=new Parent();
c.method1();
c.method2();
}
Let us discuss case by case..
Case-1
Parent p=new Parent();
p.method1();
p.method2();

here Parent class is a independent class hence
p.method1() is a valid method call
but
p.method2() is invalid as in Parent class, method2 is unavailable
so we will get a Compilation error
Error Details:
Can not find symbol
symbol:method method2()
location:Class Parent
Conclusion:
The methods very specific to child is not available to Parent .So all child specific methods can not be called with parent reference.

Case-2
Child c=new Child();
c.method1();
c.method2();

As Child class is extending to Parent class hence by default method1 is available to Child class.
hence both the method calls are valid


Conclusion:
For the valid parent child relationship, whatever the parent is having by default will be available to child


Case-3
Parent p=new Child()
p.method1();
p.method2();

This is really interesting..We are creating object of Child of type Parent.
so p.method1() call is perfectly valid
but
p.method2() we will get compilation error
Error Details:
can not find symbol
symbol:method method2()
location:Class Parent

Conclusion:
Parent reference can hold child class object but using that variable (reference) we can not call child specific methods. We can only call parent's methods


Case-4
We will get compilation error
Error Details:
incompatible types
found Parent
Required Child



Conclusion:
Child reference can not be used to hold parent class object.
The last point is keep the IS-A direction one way, Child to parent. Java supports Child to parent direction. Not the other way round.
Square IS-A shape,but not necessarily Shape IS-A Square.
That's it for today!!.
Powered by Blogger.