Java Inheritance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Java - Inheritance

Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. With the use of inheritance the information is
made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass (derived class,
child class) and the class whose properties are inherited is known as superclass (base
class, parent class).

extends Keyword

extends is the keyword used to inherit the properties of a class. Following is the syntax
of extends keyword.

Syntax

class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}

extends Keyword

extends is the keyword used to inherit the properties of a class. Following is the syntax
of extends keyword.

Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}

class Calculation {

int z;

public void addition(int x, int y) {

z = x + y;

System.out.println("The sum of the given numbers:"+z);

public void Subtraction(int x, int y) {

z = x - y;

System.out.println("The difference between the given numbers:"+z);

}
public class My_Calculation extends Calculation {

public void multiplication(int x, int y) {

z = x * y;

System.out.println("The product of the given numbers:"+z);

public static void main(String args[]) {

int a = 20, b = 10;

My_Calculation demo = new My_Calculation();

demo.addition(a, b);

demo.Subtraction(a, b);

demo.multiplication(a, b);

Why use inheritance in java

For Method Overriding (so runtime polymorphism can be achieved).

For Code Reusability.

Terms used in Inheritance

Class: A class is a group of objects which have common properties. It is a

template or blueprint from which objects are created.


Sub Class/Child Class: Subclass is a class which inherits the other class. It is

also called a derived class, extended class, or child class.

Super Class/Parent Class: Superclass is the class from where a subclass inherits

the features. It is also called a base class or a parent class.

Reusability: As the name specifies, reusability is a mechanism which facilitates

you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class.

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel
and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface


only.
Single Inheritance Example

class Animal{

void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Multilevel Inheritance Example

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();
}}

Hierarchical Inheritance Example

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();

}}

You might also like