Inheritance-in-Java .

In this session i have discussed inheritance in Java .

A class acquires the properties and behaviours from another class this process is called the inheritance . i.e a child can access all the characterstics from their parents . The main purpose of using inheritance is reusability .

Types of inheritance :

1-Single Inheritance

1-Multilevel Inheritance

1-Multiple Inheritance(Not in java)

1-Hierarchical Inheritance

1-Hybrid Inheritance(Not in java)

Inheritance Syntax :

    class derived-class extends parent-class
        //body of derived

1-Single Inheritance :

java single inheritance image

The class A is the initial class that is used as base for the class B so the A class is called the parent class or base class or super class . The class B is called the derived class or child class or sub class . In my case the B class acquires the Sum() from A class after inheritance . The B class has its own method sub() .

In single inheritance must be only one parent class and one child class . Inheritance use the extends keyword for inherit the class

1-Single Inheritance Demo

Example : Write a program that demonstrate the single inheritance in Java

    class A{
        public void sum(int x,int y){
            int result = x + y;
            System.out.println("Sum : " + result);
    class B extends A{
        public void sub(int x,int y){
            int result = x - y;
            System.out.println("Sub : " + result);
    class Test{
        public static void main(String args[]){
            B obj = new B();
            obj.sum(20, 10);
            obj.sub(20, 10);
 OUTPUT : Sum : 30
          Sub : 10

Welcome to AndroidATPoint.

back image                     next image