Inheritance
We leran anything,first we know that the question why, what, how, when.
What is inheritance?
An object of one class acting as an object of another class.
When I go for inheritance?
When there is an "IS-A" Relationship between two classes, we can go for inheritance.
How inheritance achieving in java?
We use "extends" Keyword in java for achieving inheritance.
Why should I leran inheritance?
For achieving code Reusability and efficient memory management.
Parent class=supper class, base class.
Child class=sub class, derived class.
Types of inheritance
1.single inheritance:
Refers to a child and parent class relationship. Where a child class exteds a parent class.
Example:
Actor kamalhassan and his daughter shruthi Hassan.
Program:
Public class kamlhassan{
String religion="Hindu";
Int salary=1000000;
Public static void main(string[]args) {class Teacher {
Kamalhassan khan=new kamalhassan () ;
Kh. Actor() ;
Public void actor ()
{
System.out.println("good actor ") ;
}
Public void sing()
{
System.Out.Println(" Singing songs ") ;
}
Private void bigboss()
{
System. Out. Println(" Makkal prathinithi") ;
}
Protrcted void interview ()
{
System.out.println("give interview ") ;
}
Void lifestyle () ;
{
System. Out. Println(" Luxury life") ;
}
}}
Public class shruthi extends kamlhassan{
String religion="Christian";
Int salary=100000;
Public static void main(string[]args) {
Shruthi sh=sruthi() ;
Sh. Actor() ;
Public void actor () {
System.out.println("good actor ")
;
}
Public void sing() {
system.Out.Println(" Singing songs ") ;
}
void bigboss { //not access
System. Out. Println(" Makkal prathinithi");
}
Protected void interview () {
System.out.println("give interview ") ;}}}
The above example,
1.it is not necessary for the child class(shruthi) inherit all the properties of parent class.
2.including child class we cannot access private variables and method of any object.
3 child class can have extra own methods and variables.
4.we can access protected variables, object, methods from any class in child class.
5.we can use method overriding in two different classes (act local variable first).
2.multi level inheritance:
refers to a child and parent class relationship where a class extends the child class. For example class C extends class B and class B extends class A.
Example: shruthi son's.
Program:
Public class shruthison etends shruthi {
Public static void main(string []args)
{
Shruthison ss=new shruthison() ;
Ss. Sing() ;
Ss. Actor () ;
Ss. Lifestyle () ;
System. Out. Println(ss.religion) ;
}
Private void go to school ()
{
System. Out. Println () ;
}}}
The above example, grand child can access the parent and grand parentparent properties.
3.multiple inheritance:
Java is not supporting multiple inheritance. They say multiple inheritance is leads to ambiguity or it will lead to diamond problem. After exteds key word there should be only one class name. It means., multiple inheritance have one child many parent.
4.hirarchical inheritance:
refers to a child and parent class relationship where more than one classes extends the same class. For example, classes B, C & D extends the same class A.
Example kamalhassan another daughter akshrahassan.
Program:
Public class akshara extends kamlhassan {
String religion=("Muslim");
Int salary=100000;
Akshara ak= new akshara() ;
Ak. Lifestyle () ;
Ak. Sing() ;
}
Void heroin ()
{
System. Out. Println ("wow") ;
}
The above example akshara also access kamalhassan properties.
5.Hybrid inheritance:
Combination of more than one types of inheritance in a single program. For example class A & B extends class C and another class D extends class A then this is a hybrid inheritance example because it is a combination of single and hierarchical inheritance.
Example= family tree.