2.5 Inheritance.

Inheritance :

class A //base class or super class.
{
int k = 10;
void test()
{
-
}
}

class B extends A //drived class or sub class or child class.
{
double j = 1.2;
void disp()
{
-
}
}

1. Code reusability.
2. extension.
3. modification.

A a1 = new A();
        k
     10
    test()    //A class instance


B b1 = new B1(); //without inheritance
        j
     1.2
    disp()    //B class instance


B b1 = new B(); //with inheritance.
        k
     10
    test()  

        j
     1.2
    disp()    //B instance.

Example Program1 :

class Demo1
{
int k = 121;
void test()
{
System.out.println("running test() memthod");
}
}
class Demo2 extends Demo1
{
double j = 34.12;
void disp()
{
System.out.println("running disp() memthod");
}
}

class MainClass1
{
public static void main(String args[])
{
Demo2 d1 = new Demo2();
System.out.println("k value is"+d1.k);
System.out.println("k value is"+d1.j);
d1.test();
d1.disp();
}
}

There are following inheritance:

1. Single Inheritance :
Example:

class Demo1 //base class of Demo2
{
int k;
void test();
}

class Demo2 // child class of  Demo1
{
double j;
void disp()
}

Note: Demo2 extends Demo1

2. Multi-level Inheritance.

class Demo1 //super class of Demo2
{
int k;
void test();
}

class Demo2 // sub class of  Demo1 and super of Demo3
{
double j;
void disp()
}

class Demo3 // child class of  Demo2
{
char c;
void view()
}

Note: Demo3 d1 = new Demo3();

3. Multiple inheritance.

-> not supported in java.

Syntax :
class Demo3 extends Demo1 ,Demo2
{
-
}

4. Hirarichal Inheritance :


Notes:
-> Inheritance is a property of one class to another class is known as inheritance.
-> The class from where members are inherited is known as base class or super class.
-> The class to which members are inherited is known as drived or sub class.
-> The subclass always inherited only the non-static properties of super class. it will never inherite static property , private , non-static members.
-> Whenever   we create an  instance of sub-class it will always have the property of its super class.

There are 4-type of inheritance :
1. Single - Inheritance : in this type of inheritance sub-class inherit from only one super class.
2. Multi-Level Inheritance : in this type of inheritance sub-class inherit from a super class which is a sub class of another class.
3. Multiple-Inheritance : in this type of inheritance sub class inherite from more then one super class java doesn't support multiple inheritance.
4. Hirarchical Inheritance : in this type the super class as more than one sub-class this type of inheritance used to consume achive to generalization.

Inheritance used to achieve :

1. Code reusability.
2. extands.
3. modification or inhancement.
Inheritance is  used to built an easier relationship between classes.

class Sample1
{
int k;
sample() //constructor.
{
k = 25;
}
}
class Sample2 extends Sample1
{
super()
{
-
}
}

Sample2 s1 = new sample2();

        k
     10
    Sample2 instance


Super() :
-> used to call super class constructor.
-> call can be implicite or explicite.
-> it should be final statement of constructor body.
-> only one statement is allowed.

Implicite Constructor Call :

class Sample1
{
int k;
sample()
{
System.out.println("running sample constructor");
k = 25;
}
}
class Sample2 extends Sample1
{
double j;
sample2()
{
System.out.println("running sample2() constructor");
j = 4.5;
}
}

class MainClass2
{
public static void main(String args[])
{
System.out.println("Program Started");
Sample2 s1 = new Sample2();
System.out.println("k value:"+s1.k);
System.out.println("k value:"+s1.j);
System.out.println("Program Ended");
}
}


Constructor Call :

In an inheritence program whenever we create the object of subclass. The subclass constructor must make a call to it's superclass constructor.

--> The call can be implicite call or explicite call.

--> If compiler makes a call to the super class constructor then it is known as implicite call.

--> If the programmer does the super class construvtor call then it is known as explicite call.

--> Sub class constructor can call super class constructor by using super statement.

--> The super statement should be used only in the constructor body and it must be the first statement.

--> We cannot used move than one statement.

--> Super statement can be used to make a call either no argument constructor or parameterize             constructor.

--> The compiler perform implicite call only when the super class is having zero argument constructor.

--> If in case the super class doesn't have zero argument constructor or the sub-class wants to make a call to parameterized constructor of super class then we should go for explicite call.

Costructor Chaining:

Is a phenomena happens in inheritance where the sub-class constructor makes a call to the super-class constructor and that super class constructor makes a call to it's super class constructor call can be done either implicitely or explicitely just chaining is required to do the initialization of inherited data members.

Note:

Each java class created in java language must have a super class either defined by programmer or defined by compiler.
                     By default the class inherite the property of object class.
object class is a rude class in java library .
java doesn't support multiple-inheritance because
1. The sub-class constructor can not make a call to move than one super class constructor because multiple super statement not available.
2. If links to ambguilty of diamond problem.





 Notes :


 -> A constructor of  a class can  make a call to the another constructor of same class by using 'this' statement. 
-> This statement should be alway the first statement of constructor body.
-> It should be used explicitely multiple this statement are not allowed.
-> Recursively constructor call are not allowed.
-> A constructor can either make a call the super class constructor or its own class constructor or super constructor not both.

-> Java provides a keyword by name 'super' which is used to refer the super class members from subclass.
->The super keywords should be used either constructor body or in non-static context.











Comments

Popular Posts