OOPs Concepts: Inheritance in Java

 The process of acquiring the properties of one class through another class is known as Inheritance.

It provides the form of re-usability that means we can add extra features to the existing class by deriving a new class from the existing one without modifying the original class.

The original class is called super class (or) Base class (or) Parent class. The new class is called as sub class (or) derived class (or) child class.

The sub class can Inherit the features of the super class.


Defining a sub class:-

Syntax:-

class SubClassName extends SuperClassName{

    Variable Declaration;

    Method Definitions;

}

The keyword extends signifies that the properties of the superclassname are extended to the subclassname. The subclass will now contain its own variables and methods as well as those of the superclass.


Ex:-
class Result extends Student
{
    int s1,s2,s3,total;
    float avg;
    void getResult()
    {
        ---
    }
    void calculate()
    {
        ---
    }
    void putResult()
    {
        ----
    }
}


  • Inheritance available in different forms those are

    1. Single Inheritance
    2. Multilevel Inheritance
    3. Hierarchical Inheritance
    4. Multiple Inheritance(available only with interfaces in java)

Comments

Popular posts from this blog

How to Use Crome Developer Tools: Crome Developer Tools