You are on page 1of 19

Java Inheritance

Inheritance in Java Definition:

● Inheritance in Java is a concept that acquires the


properties from one class to other classes; for
example, the relationship between father and son.
● The new class that is created is known as subclass

(child or derived class)

● The existing class from where the child class is

derived is known as superclass (parent or base class).

Inheritance Syntax in Java

class derived_class extends base_class

//methods

//fields

General format for Inheritance

class​ superclass

{
// superclass data variables

// superclass member functions

class​ subclass ​extends​ superclass

// subclass data variables

// subclass member functions

Extends keyword in Java

Example:

class Base

public void M1()

System.out.println(“ Base Class Method ”);


}

class Derived extends Base

public void M2()

System.out.printIn(“ Derived Class Methods “);

class Test

public static void main(String[] args)

Derived d = new Derived(); // creating object

d.M1(); // print Base Class Method

d.M2(); // print Derived Class Method


}

Output:

Example2:

class Animal {

// field and method of the parent class

String name;

public void eat() {

System.out.println("I can eat");

}
// inherit from Animal

class Dog extends Animal {

// new method in subclass

public void display() {

System.out.println("My name is " + name);

class Main {

public static void main(String[] args) {

// create an object of the subclass

Dog labrador = new Dog();

// access field of superclass


labrador.name = "Rohu";

labrador.display();

// call method of superclass

// using object of subclass

labrador.eat();

Output

My name is Rohu

I can eat

Why use Inheritance in Java?

The main advantage of inheritance is code reusability

and also method overriding (runtime polymorphism).


Inheritance is also known as the IS-A relationship.

Types of Inheritance in Java

The different types of Inheritance are:

● Single Inheritance

● Multiple Inheritance

● Multi-Level Inheritance

● Hierarchical Inheritance

● Hybrid Inheritance

Single Inheritance

Creating subclasses from a single base class is called

single inheritance.

Program Example:
class A

int a, b;

void display()

System.out.println(“Inside class A values =”+a+”

”+b);

class B extends A

int c;
void show()

System.out.println(“Inside Class B values=”+a+”

“+b+” “+c); }

class SingleInheritance

public static void main(String args[])

B obj = new B(); //derived class object

obj.a=10;

obj.b=20;

obj.c=30;
obj.display();

obj.show();

Multiple Inheritance in Java

Defining derived class from numerous base classes is

known as ‘Multiple Inheritance’. In this case, there is

more than one superclass, and there can be one or

more subclasses.
Multiple inheritances are available in object-oriented

programming with C++, but it is not available in Java.

Multi-Level Inheritance in Java

In Multi-Level Inheritance in Java, a class extends to


another class that is already extended from another
class. For example, if there is a class A that extends
class B and class B extends from another class C, then
this scenario is known to follow Multi-level
Inheritance.

Example:
class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
Output
Inside display
Inside area
Inside volume

Example:2
class Animal{
void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}
class BabyDog extends Dog{

void weep(){System.out.println("weeping...");}

class TestInheritance2{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}}

Output:

weeping...
barking...
eating...

Hierarchical Inheritance in Java

When two or more classes inherits a single class, it is


known as hierarchical inheritance.

class Animal{
void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

class Cat extends Animal{

void meow(){System.out.println("meowing...");}

class TestInheritance3{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

//c.bark();//C.T.Error

}}

Output:

meowing...

Eating…
Example 2:

class A

public void methodA()

System.out.println("method of Class A");

class B extends A

public void methodB()

System.out.println("method of Class B");

class C extends A

public void methodC()


{

System.out.println("method of Class C");

class D extends A

public void methodD()

System.out.println("method of Class D");

class JavaExample

public static void main(String args[])

B obj1 = new B();

C obj2 = new C();

D obj3 = new D();

//All classes can access the method of class A


obj1.methodA();

obj2.methodA();

obj3.methodA();

Output:

method of Class A

method of Class A

method of Class A

You might also like