Tuesday, 31 January 2017

Inheritance In Java

Inheritance in Java

Inheritance in java is a mechanism in which one class or object acquires all the properties(methods and fields) and behaviors of parent class or object.
extends is the keyword used to inherit the properties of a class.

Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java

- For Code Reusability.
- For Method Overriding (so runtime polymorphism can be achieved).

Point to be remember regarding Inheritance.

- Constructor and initilization block (SIB,IIB) are not inherit to sub class
- Every constructor should have the first statement either super() or this() calling statement
- If we are not keeping the super() or this() as a first statement in constructor ,compiler will keep super().
- Parent class also have super() keyword to Object class.

Following example demonstrate inheritance , constructor, this() and super() keyword.
class A
{
    A(double d)
    {
        this();
        System.out.println("A(double)");   
    }
    A()
    {
        System.out.println("A()");
    }
    void test()
    {
        System.out.println("test()");
    }
}
class B extends A
{
    B()
    {
        super(9.3);
        System.out.println("B()");
    }
}
public class Demo
{
    public static void main(String args[])
    {
        B b=new B();
        b.test();
    }   
}

Output:
A()
A(double)
B()
test()


Programs for Practice on Inheritance

Program # 1

class A
{
    int i;
}
class B extends A
{
    int j;
    public static void main(String[] args)
    {
        B obj1=new B();
        System.out.println(obj1.i);
        System.out.println(obj1.j);
    }
}

Output:
0
0

Program # 2
Note: inside class D there are 3 members

class C
{
    int i;
    void test()
    {
        System.out.println("Test");
    }
}
class D extends C
{
    int j;
}
class E
{
    public static void main(String[] args)
    {
        D d1=new D();
        d1.i=10;
        d1.j=20;
        d1.test();
        System.out.println(d1.i);
        System.out.println(d1.j);
    }
}

Output:
Test
10
20

Program # 3
class F
{
    int i;
    static int j;
    void test1()
    {
        System.out.println("Test1");
    }
    static void test2()
    {
        System.out.println("Test2");
    }
}
class G extends F
{
    int k;
    static void test3()
    {
        System.out.println("Test3");
    }
    public static void main(String[] args)
    {
      F.j=10;
      F.test2();
      G.test2();
      G.test3();
      System.out.println(G.j);
      F f1=new F();
      f1.i=20;
      f1.test1();
      G g1=new G();
      g1.i=30;
      g1.k=50;
      g1.test1();
    }
}

Output:
Test2
Test2
Test3
10
Test1
Test1

Program # 4

class H
{
    int x;
}
class I extends H
{
    int y;
}
class J extends I
{
    int z;
}
class Manager
{
    public static void main(String[] args)
    {
        H h1=new H();
        I i1=new I();
        J j1=new J();
        System.out.println(h1.x);
        System.out.println(i1.x);
        System.out.println(j1.x);
        System.out.println(i1.y);
        System.out.println(j1.y);
        System.out.println(j1.z);
    }
}

Note: while creating the object of J all class H,I and J will load in memory

Output:
all 0

Program # 5
class K
{
    K()
    {
    //super();  //compiler keep super() internally
        System.out.println("K()");
    }
}
class L extends K
{
    L()
    {
    //super();  //compiler keep super() internally
        System.out.println("L()");
    }
}
class Manager
{
    public static void main(String[] args)
    {
        K k1=new K();
        System.out.println("------");
        L l1=new L();
        System.out.println("------");
    }
}
Note:
- constructor and initilization block (SIB,IIB) are not inherit to sub class
- every constructor should have the first statement either super() or this() calling statement
- if we are not keeping the super() or this() as a first statement in constructor ,compiler will keep super().
- even a parent class have super() statement to Object class.

Output:
K()
------
K()
L()
------

Program # 6
Note: Total 4 constructor are executing in this program.

class M extends Object
{
    M()
    {
        super();
        System.out.println("M()");
    }
}
class N extends M
{
    N()
    {
        super();
        System.out.println("N()");
    }
}
class O extends N
{
    O()
    {
        super();
        System.out.println("O()");
    }
}
class Manager2
{
    public static void main(String[] args)
    {
        M m1=new M();
        System.out.println("------");
        N l1=new N();
        System.out.println("------");
        O o1=new O();
        System.out.println("------");
    }
}


Output:
M()
------
M()
N()
------
M()
N()
O()
------

Program # 7

class P
{
    P()
    {
        System.out.println("P()");
    }
}
class Q extends P
{
    Q()
    {
        System.out.println("Q()");
    }
}
class R extends Q
{
    R()
    {
        System.out.println("R()");
    }
}
class S extends R
{
    S()
    {
        System.out.println("S()");
    }
}
class T extends S
{
    T()
    {
        System.out.println("T()");
    }
    public static void main(String[] args)
    {
        P m1=new P();
        System.out.println("------");
        Q l1=new Q();
        System.out.println("------");
        R o1=new R();
        System.out.println("------");
        S s1=new S();
        System.out.println("------");
        T t1=new T();
        System.out.println("------");
    }
}


Output:
R()
S()
------
P()
Q()
R()
S()
T()
------

Program # 8
class A
{
    A()
    {
        System.out.println("A()");
    }
    A(int i)
    {
        System.out.println("A(int)");
    }
}
class B extends A
{
    B()
    {
        System.out.println("B()");
    }
    B(int i)
    {
        System.out.println("B(int)");
    }
}
class Manager
{
    public static void main(String[] args)
    {
      A m1=new A();
      System.out.println("------");
      A l1=new A(20);
      System.out.println("------");
      B o1=new B();
      System.out.println("------");
      B s1=new B(30);
      System.out.println("------");
    }
}


Output:
A()
------
A(int)
------
A()
B()
------
A()
B(int)
------

Program # 9
class E
{
    E()
    {
        System.out.println("E()");
    }
}
class F extends E
{
    F(int i)
    {
        System.out.println("F(int)");
    }
}
class Manager2
{
    public static void main(String[] args)
    {
      E m1=new E();
      System.out.println("------");
      F l1=new F(20);
    }
}

Output:
E()
------
E()
F(int)

Program # 10
class G
{
    G(int i)
    {
        System.out.println("G(int)");
    }
}
class H extends G
{
    H()
    {
        //super(10); if remove this super(10) will give compiler error
        System.out.println("H()");
    }
}
class Manager3
{
    public static void main(String[] args)
    {
      G g1=new G(90);
      System.out.println("------");
      H h1=new H();
    }
}

Output:
H.java:11: cannot find symbol
symbol : constructor G()
location: class G
{
^
1 error
------------------
G(int)
------
G(int)
H()

Program # 11
class I
{
    I(double j)
    {
        System.out.println("I(double)");
    }
}
class J extends I
{
    J()
    {
        super(9.9);
        System.out.println("J()");
    }
    J(double d){
        super(d);
        System.out.println("J(double)");
    }
}
class Manager4
{
    public static void main(String[] args)
    {
      I i1=new I(9.9);
      System.out.println("----------");
      J j1=new J();
      System.out.println("----------");
      J j2=new J(3.4);
    }
}

Output:
I(double)
----------
I(double)
J()
----------
I(double)
J(double)

Program # 12
class K
{
    K()
    {
        System.out.println("K()");
    }
    {
        System.out.println("K-IIB");
    }
}
class L extends K
{
    L()
    {
        System.out.println("L()");
    }
    {
        System.out.println("L-IIB");
    }
}
class Manager5
{
    public static void main(String[] args)
    {
        K k1=new K();
        System.out.println("----------");
        L l1=new L();
        System.out.println("----------");
    }
}

Output:
K-IIB
K()
----------
K-IIB
K()
L-IIB
L()
----------

Program # 13
class M
{
    M()
    {
        System.out.println("M()");
    }
    {
        System.out.println("M-IIB-1");
    }
    {
        System.out.println("M-IIB-2");
    }
}
class N extends M
{
    N()
    {
        System.out.println("N()");
    }
    {
        System.out.println("N-IIB-1");
    }
    {
        System.out.println("N-IIB-2");
    }
}
class Manager6
{
    public static void main(String[] args)
    {
      M k1=new M();
      System.out.println("----------");
      N l1=new N();
      System.out.println("----------");
    }
}

Output:
M-IIB-1
M-IIB-2
M()
----------
M-IIB-1
M-IIB-2
M()
N-IIB-1
N-IIB-2
N()
----------

Program # 14
class O
{
    {
        System.out.println("O-IIB");
    }
    O()
    {
        System.out.println("O()");
    }
}
class P extends O
{
    P()
    {
        System.out.println("P()");
    }
    {
        System.out.println("P-IIB");
    }
    P(int i)
    {
        this();//IT will call constructor of same class
        System.out.println("P(int)");
    }
}
class Manager7
{
    public static void main(String[] args)
    {
        O m1=new O();
        System.out.println("------");
        P o1=new P();
        System.out.println("------");
        P s1=new P(30);
        System.out.println("------");
    }
}

Output:
O-IIB
O()
------
O-IIB
O()
P-IIB
P()
------
O-IIB
O()
P-IIB
P()
P(int)
------

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...