Java Inheritance
IS-A relation:
• Also known as Inheritance
• By using extends keyword we can implement IS-A relationship.
• Re usability is the benefit of IS-A relationship
Inheritance is accepted up to certain level but after reaching problems. Because for every child class object, internally all the parent objects will be created in the inheritance tree.
In the real time it is recommended to have inheritance up to 8 to 10 levels only. Beyond that it is not suggestible.
is a relation Example:
class Superclass
{
void display()
{
System.out.println("Hi");
}
}
class InheritanceExample extends Superclass
{
public static void main(String arg[])
{
InheritanceExample ie=new InheritanceExample();
ie.display();
}
}
HAS-A relation:
• Also known as composition or Aggregation.
• By using new operator we can implement HAS-A relationship.
• Reusability(CBM->Component Based Model)is the benefit of HAS–A relationship.
• The limitation of HAS-A relation ship is , we are increasing the dependency between the classes. As a result, the maintenance of the code becomes complex or costly.
has a relation Example:
class Car
{
Engine e= new Engine();
. …….
.......
}
class Engine
{
m1(){}
m2(){}
}
Car class has Engine reference. Hence Car allowed using all the features of engine. But without Engine object we can’t create Car object.
0 comments:
Post a Comment