Home | | Object Oriented Programming | Java - Inheritance

Chapter: Object Oriented Programming(OOP) : Overview of Java

Java - Inheritance

Reusability is achieved by INHERITANCE

INHERITANCE

 

1.     Reusability is achieved by INHERITANCE

 

2.     Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes.

 

3.     The class whose properties are extended is known as super or base or parent class.

 

4.     The class which extends the properties of super class is known as sub or derived or child class

 

5.     A class can either extends another class or can implement an interface

 

class B extends A { …..  }

A super class

B sub class

class B implements A {  ….. }

A interface

B sub class

Various Forms of Inheritance

 

7.1Defining a Subclass Syntax :

 

class <subclass name> extends <superclass name>

 

{

 

variable declarations; method declarations;

 

}

         Extends keyword signifies that properties of the super class are extended to sub class

         Sub class will not inherit private members of super class

Access Control

 

 

Inheritance Basics

When super class has a Unparametrized constructor

 

class A

{

A()

 

{

System.out.println("This is constructor of class A");

}

 

} // End of class A class B extends A

 

{

 

B()

{

 

super();

 

 

 

System.out.println("This is constructor of class B");

}

 

} // End of class B class inhtest

 

{

public static void main(String args[])

{

B b1 = new B();

}

}

OUTPUT

 

This is constructor of class A This is constructor of class B class A

 

{

A()

{

System.out.println("This is class A");

}

}

class B extends A

{

B()

{

System.out.println("This is class B");

}

}

class inherit1

{

public static void main(String args[])

{

B b1 = new B();

}

}

 

File Name is xyz.java /*

 

E:\Java>java inherit1 This is class A

 

This is class B E:\Java>

 

*/ class A

{

private A()

{

System.out.println("This is class A");

 

}

}

class B extends A

{

B()

{

System.out.println("This is class B");

}

}

class inherit2

{

public static void main(String args[])

{

 

B b1 = new B();

}

}

/*

E:\Java>javac xyz1.java

xyz1.java:12: A() has private access in A

{

^

 

1 error */ class A

{

 

private A()

{

System.out.println("This is class A");

}

A()

{

System.out.println("This is class A");

}

}

 

class B extends A

{

B()

 

{

System.out.println("This is class B");

}

}

class inherit2

{

public static void main(String args[])

{

B b1 = new B();

 

} }

/*

E:\Java>javac xyz2.java

 

xyz2.java:7: A() is already defined in A A()

 

^

xyz2.java:16: A() has private access in A

{

^

 

2 errors */

 

When Super class has a parametrized constructor. class A

 

{

 

private int a; A( int a)

 

{

this.a =a;

 

System.out.println("This is constructor of class A");

}

}

class B extends A

{

 

private int b; private double c; B(int b,double c)

 

{

this.b=b;

this.c=c;

System.out.println("This is constructor of class B");

}

}

 

B b1 = new B(10,8.6); D:\java\bin>javac inhtest.java inhtest.java:15: cannot find symbol symbol : constructor A()

 

location: class A

 

{

^

 

1 errors class A

{

 

private int a; protected String name; A(int a, String n)

 

{

 

this.a = a; this.name = n;

}

 

void print()

{

System.out.println("a="+a);

}

}

class B extends A

{

 

int b; double c;

B(int a,String n,int b,double c)

 

{

super(a,n);

 

this.b=b; this.c =c;

}

void show()

{

//System.out.println("a="+a);

print();

System.out.println("name="+name);

System.out.println("b="+b);

System.out.println("c="+c);

}

} class A

{

 

private int a; A( int a)

 

{

this.a =a;

System.out.println("This is constructor of class A");

}

 

void show()

{

System.out.println("a="+a);

 

}

void display()

{

System.out.println("hello This is Display in A");

}

}

class B extends A

{

private int b;

 

private double c; B(int a,int b,double c)

 

{

super(a);

this.b=b;

this.c=c;

System.out.println("This is constructor of class B");

}

void show()

{

super.show();

System.out.println("b="+b);

System.out.println("c="+c);

 

display();

}

}

class inhtest1

{

public static void main(String args[])

{

 

B b1 = new B(10,8,4.5); b1.show();

 

}

}

 

/* OutPut D:\java\bin>java inhtest1

 

This is constructor of class A This is constructor of class B a=10

 

b=8

c=4.5

 

hello This is Display in A */

 

 

7.2 Types

Single inheritance

 

Class A

{

public void methodA()

{

System.out.println("Base class method");

}

}

 

Class B extends A

{

public void methodB()

{

System.out.println("Child class method");

}

public static void main(String args[])

{

B obj = new B();

 

obj.methodA(); //calling super class method

obj.methodB(); //calling local method

}

}

 

Multiple Inheritance

 

“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent.

 

The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.

 

Multilevel Inheritance

 

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. For more details and example refer – Multilevel inheritance in Java.


Multilevel Inheritance example program in Java

Class X

 

{

public void methodX()

 

{

System.out.println("Class X method");

}

}

Class Y extends X

{

public void methodY()

{

System.out.println("class Y method");

}

}

Class Z extends Y

{

public void methodZ()

{

System.out.println("class Z method");

}

public static void main(String args[])

{

Z obj = new Z();

 

obj.methodX(); //calling grand parent class method

obj.methodY(); //calling parent class method

obj.methodZ(); //calling local method

 

}

}

 

Hierarchical 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. Read More at – Hierarchical Inheritance in java with example program.


 

Hybrid Inheritance

 

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.

 

Read the full article here – hybrid inheritance in java with example program.





Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming(OOP) : Overview of Java : Java - Inheritance |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.