DEV Community

akhilreddy0401
akhilreddy0401

Posted on

2

Inheritance in Java

Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class.

Important terminology:

Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
How to use inheritance in Java

The keyword used for inheritance is extends.

Syntax :

class derived-class extends base-class

{

//methods and fields

}

Example:
In the below example of inheritance, class Bicycle is a base class, class MountainBike is a derived class that extends Bicycle class and class Test is a driver class to run program.

Java
// Java program to illustrate the
// concept of inheritance

// base class

class Bicycle {

// the Bicycle class has two fields

public int gear;

public int speed;


// the Bicycle class has one constructor

public Bicycle(int gear, int speed)

{

    this.gear = gear;

    this.speed = speed;

}


// the Bicycle class has three methods

public void applyBrake(int decrement)

{

    speed -= decrement;

}


public void speedUp(int increment)

{

    speed += increment;

}


// toString() method to print info of Bicycle

public String toString()

{

    return ("No of gears are " + gear + "\n"

            + "speed of bicycle is " + speed);

}
Enter fullscreen mode Exit fullscreen mode

}

// derived class

class MountainBike extends Bicycle {

// the MountainBike subclass adds one more field

public int seatHeight;


// the MountainBike subclass has one constructor

public MountainBike(int gear, int speed,

                    int startHeight)

{

    // invoking base-class(Bicycle) constructor

    super(gear, speed);

    seatHeight = startHeight;

}


// the MountainBike subclass adds one more method

public void setHeight(int newValue)

{

    seatHeight = newValue;

}


// overriding toString() method

// of Bicycle to print more info

@Override public String toString()

{

    return (super.toString() + "\nseat height is "

            + seatHeight);

}
Enter fullscreen mode Exit fullscreen mode

}

// driver class

public class Test {

public static void main(String args[])

{


    MountainBike mb = new MountainBike(3, 100, 25);

    System.out.println(mb.toString());

}
Enter fullscreen mode Exit fullscreen mode

}
Output
No of gears are 3
speed of bicycle is 100
seat height is 25
In the above program, when an object of MountainBike class is created, a copy of all methods and fields of the superclass acquire memory in this object. That is why by using the object of the subclass we can also access the members of a superclass.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay