Inheritance:
The process of getting properties and behaviors from one class to another class is called inheritance.
- Properties: Variables
- Behaviors: Methods
- The main purpose of the inheritance is code extensibility whenever we are extending automatically the code is reused.
- In inheritance one class giving the properties and behavior and another class is taking the properties and behavior.
- Inheritance is also known as is-a relationship means two classes are belongs to the same hierarchy.
- By using extends keyword we are achieving inheritance concept.
- In the inheritance the person who is giving the properties is called parent the person who is taking the properties is called child.
- To reduce length of the code and redundancy of the code sun peoples introducing inheritance concept.
Types of inheritance:
Single inheritance:
- Class A: Extending the properties from single parent class to B extends A single child class is called single inheritance.
- Class B: Class A is Parent class, Class B is Child class.
1. Multilevel inheritance: Class A, B extends A, Class B one super and one sub class at each and every level. C extends B Class C.
2. Multiple inheritance:The process of getting properties and behaviors form more than one super class to the one child class. The multiple inheritance is not possible in the java language so one class can extends only one class at time it is not possible to extends more than one class at time.
- Class B extends A - Possible
- Class C extends B - Possible
- Class C extends A, B - Not possible
- Class A Class B
- C extends A, B
- Class C
3. Hierarchical inheritance: The process of getting properties and behaviors from one super class to the more than one sub classes is called hierarchical inheritance.
- Class A
- B extends A C extends A D extends A
- Class B Class C Class D
4. Hybrid inheritance: Combination of any two inheritances is called as hybrid inheritance. If are taking the multilevel and hierarchical that combination is called hybrid inheritance.
- Class A
- B extends A
- Class B
- C extends B
- Multilevel inheitance Class C
- Hierarchical inheritance
- D extends C E extends C
- Class D class E
- Before inheritance :-
- Class A
{
Void m1();
Void m2();
};
class B
{
Void m1();
Void m2();
Void m3();
Void m4();
};
class C
{
Void m1();
Void m2();
Void m3();
Void m4();
Void m5();
Void m6();
}
------------------------------------------------------------------------------------------------------------
After inheritance:
class A
{
Void m1() parent class/
Void m2() super class/
Void m3() base class
}
class B extends A
{ child class/
Void m4() sub class
Void m5() derived class
}
Class C extends B
{
Void m6();
}
Note 1:
It is possible to create objects for both parent and child classes.
a. If we are creating object for parent class it is possible to call only parent specific methods.
A a=new A();
a.m1();
a.m2();
a.m3();
b. if we are creating object for child class it is possible to call parent specific and child specific.
B b=new B();
b.m1();
b.m2();
b.m3();
b.m4();
b.m5();
c. if we are creating object for child class it is possible to call parent specific methods and child specific methods.
C c=new C();
c.m1();
c.m2();
c.m3();
c.m4();
c.m5();
c.m6();
Ex:
class Parent
{
public void m1()
{
System.out.println("m1-method");
}
public void m2()
{
System.out.println("m2-method");
}
}
------------------------------------------------------------------------------------------------------
class Child extends Parent
{
public void m3()
{
System.out.println("m3-method");
}
public static void main(String[] args)
{
Child c=new Child();
c.m1();
c.m2();
c.m3();
Parent p=new Parent();
p.m1();
p.m2();
}
}
Note:
1. Every class in the java programming is a child class of object.
2. The root class for all java classes is Object class.
3. The default package in the java programming is java.lang package.
Both declaration are same:-
class Test
{
};
class String
{
};
class Test extends Object
{
};
class String extends Object
{
};