Inheritance
One of the most Basic building blocks of object oriented Programming is inheritance with the help of inheritance Programming task is becoming very simple and lucid ,all we know importance of inheritance a d Java support various types of inheritance excpet Multiple inheritance
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class whose properties are inherited is known as superclass (base class, parent class) and the class which inherits the properties of other is known as subclass (derived class, child class).
Inheritance represents the IS-A relationship, also known as parent-child relationship.
extends Keyword
extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.
class Super
{
String name;
}
class Sub extends Super
{
//methods and fields
String name;
void show()
{
super.name = "Super Class"; //name of Super class
name = "Sub Class"; //name of Sub class
}
}
super Keyword
{
String name;
}
class Sub extends Super
{
//methods and fields
String name;
void show()
{
super.name = "Super Class"; //name of Super class
name = "Sub Class"; //name of Sub class
}
}
super Keyword
In Java, super keyword is used by a subclass whenever it need to refer to its immediate super class.
1. It is used to differentiate the members of superclass from the members of subclass, if they have same names.
2. It is used to invoke the superclass constructor from subclass.
super.variable
super.method();
super.method();
Purpose of Inheritance
1. To promote Code Reusability.
2. To use Polymorphism.
Types of Inheritance
1. Single Inheritance
When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
class A {
}
class B extends A {
class B extends A {
}
2. Multilevel Inheritance
When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
class A {
}
class B extends A {
class B extends A {
}
class C extends B {
class C extends B {
}
3. Heirarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.
class A {
}
class B extends A {
class B extends A {
}
class C extends A {
class C extends A {
}
class D extends A {
class D extends A {
}
NOTE : Multiple inheritance is not supported in java.
Q. Why multiple inheritance is not supported in java???
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
Example:
class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B //suppose if it were
{
Public Static void main(String args[])
{
C obj=new C();
obj.msg();
//Now which msg() method would be invoked?
}
}
Output:
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B //suppose if it were
{
Public Static void main(String args[])
{
C obj=new C();
obj.msg();
//Now which msg() method would be invoked?
}
}
Output:
Compile Time Error
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Example:
class Employee{
int id;
String name;
Address address; //Address is a class
...
}
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.
int id;
String name;
Address address; //Address is a class
...
}
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.
Why use Aggregation??
For Code Reusability.
When use Aggregation??
1. Code reuse is also best achieved by aggregation when there is no IS-A relationship.
2. Inheritance should be used only if the relationship IS-A is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
No comments:
Post a Comment