javascript
Brief description  about Online courses   join in Online courses
OR

Inheritance - A Beginning

Tabresh  Allam
Tabresh Allam
Senior Software Engineers

Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the base
class or superclass) with another class (called the derived class or subclass). In Java,

inheritance is used for two purposes:

1. class inheritance - create a new class as an extension of another class, primarily for the purpose

of code reuse. That is, the derived class inherits the public methods and public data of the

base class. Java only allows a class to have one immediate base class, i.e., single class

inheritance.

2. interface inheritance - create a new class to implement the methods defined as part of an

interface for the purpose of subtyping. That is a class that implements an interface “conforms

to” (or is constrained by the type of) the interface. Java supports multiple interface inheritance.

In Java, these two kinds of inheritance are made distinct by using different language syntax. For

class inheritance, Java uses the keyword extends and for interface inheritance Java uses the

keyword implements.

public class derived-class-name extends base-class-name {

// derived class methods extend and possibly override

// those of the base class

}

public class class-name implements interface-name {

// class provides an implementation for the methods

// as specified by the interface

}

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

In Java, Objects have got no superclass only classes have Superclass. Every class has got only one Superclass (not two or three), if the class doesn’t have any superclass then the class is a subclass of an object.


Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object. In other words, Object is the parent class of all classes.


We need not write the code for a class again and again if we know that a similar code is already available in some other class. In this case we just inherit the fields and methods from the super class. Please note that the constructors are not members of a class, so they are not inherited by the subclass.


An example program of Inheritance:


class MyBase    {

    private int x;
    public MyBase(int x) {
    this.x = x;
}

    public int getX()    {
    return x;
}
    public void show()    {
    System.out.println("x=" + x);
    }
}

class MyDerived extends MyBase    {

    private int y;
   
    public MyDerived(int x) {
        super(x);
}
 
    public MyDerived(int x, int y) {
        super(x);
        this.y = y;
}

 public int getY()     {
     return y;
}
 
public void show()    {
    System.out.println("x = " + getX());
    System.out.println("y = " + y);
    }
}

public class Inheritance1    {

    public static void main(String[] args)    {
        MyBase b = new MyBase(2);
        b.show();
        MyDerived d = new MyDerived(3, 4);
        d.show();
    }
}

I

Write your comment now