Inheritance Concept Simplified In Java


Inheritance allows descendants of  a class (known as child class or subclass) to inherit all of its member elements and methods from its ancestor(Known as Parent class or super class).
The advantage of Inheritance is that it supports incremental development by allowing us to write new code without affecting the previously deployed code. By inheriting from an existing functional class,we add new methods and data members. This mechanism leaves the actual code as it is and all changes happens to be in defined  in derived class . This also helps in tracking the bugs easily.
Another aspect of Inheritance is that we don't need the source code of the base class. Rather we need to import that class and write an extend keyword to our class.No copy paste is required.
Inheritance works as a building block providing incremental support which is very much sustainable.

An Example

class MyClass{
    public int x,y;
    MyClass(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
}
class MyAnotherClass extends MyClass{
    int z;
    //inherits x,y from 
    MyAnotherClass(int x,int y,int z)
    {
    //code duplication
    //this.x=x;
    //this.y=y;
    //instead use super
    super(x,y);         
    this.z=z;
    }
    void displayValues()
    {
        System.out.println(x+y+z);
    }
}
public class TestInheritance{
    public static void main(String args[])
    {
        MyAnotherClass mac=new MyAnotherClass(3,2,2);
        mac.displayValues();
    }
}
 
relax!! the code won't compile if we do not activate super() and activate the two commented lines. This is just to show how we can do repetition of data if we do not take help from Inheritance.To avoid the repetition of code ,java uses a special keyword called Super,this can be used to refer directly to super class constructor.
this is another keyword user to tell java compiler that this will refer to current object.
Let us see one more example

class Toy{
    void createNoice()
    {
        System.out.println("DUM DUM");
    }
}
class RailToy extends Toy{
    void createNoice()
    {
        System.out.println("JHIK JHIK");
    } 
}
class BusToy extends Toy{
    static boolean noHorn;
    void createNoice()
    { 
        if(noHorn)
        super.createNoice();
        else
        System.out.println("GOOO");
    }
}
public class MyToy{
    public static void main(String args[])
    {
        RailToy rt=new RailToy();
        BusToy bt=new BusToy();
        bt.noHorn=true;
        bt.createNoice();
        rt.createNoice();
        bt.noHorn=false;
        bt.createNoice();
    }
    
}
 
Output of the code:
$javac MyToy.java
$java -Xmx128M -Xms16M MyToy
DUM DUM
JHIK JHIK
GOOO
Here createNoice() method is overridden in the respective classes,with a condition that if noHorn is set to true it will invoke the Toy class's createNoice() method.
Inheritance Concept Simplified In Java Inheritance Concept Simplified In Java Reviewed by Animesh Chatterjee on November 16, 2018 Rating: 5

No comments:

Powered by Blogger.