Inheritance in Java

by | May 19, 2021 | Java

Home » Java » Inheritance in Java

Inheritance in Java

Inheritance is one of the most fundamental features of Object-Oriented Programming. As the name suggests, inheritance is the feature by which one class inherits the fields and attributes of another class. Some of the important terminologies associated with inheritance are:

  • Superclass: A superclass is a class whose features are inherited. It is also often called a base or a parent class.
  • Subclass: Subclass is the class that inherits properties from the superclass. This is also known as the child class. While the subclass inherits methods and fields from the superclass, it also has methods and fields of its own.
  • Reusability: By inheriting fields and methods from a class, reusability of code is also taking place. This is one particular trait that proves to be very beneficial when talking in terms of computational efficiency for long codes. Not only can subclasses use their own properties but also reuse code from the superclasses.

Syntax

When you want one class to inherit from another class, the keyword to be used is ‘extends’.

class derived-class extends base-class
{
//method and fields
}

 

Sample Program

This program has a Bicycle class which is the superclass. The MountainBike Class extends from the Bicycle class and the Test class drives the program.

class Bicycle
{
public int gear;
public int speed;
public Bicycle (int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
public void applyBrake (int decrement)
{
speed -= decrement;
}
public void speedup (int increment)
{
speed += increment;
}
public String to String ()
{
return (“No of gears are” + gear + “\n” + “speed of the bicycle is” + speed);
}
}
class MountainBike extends Bicycle
{
public int seatHeight;
public MountainBike (int gear, int speed, int startHeight)
{
super (gear, speed);
seatHeight = startHeight;
}
public void setHeight (int newValue)
{
seatHeight = newValue;
}
@Override public String toString()
{
return (super.toString() + “\nseat height is ” + seatHeight);
}
}
public class Test
{
public static void main (String args [])
{
MountainBike mb = new MountainBike (3, 100, 25);
System.out.println (mb.toString());
}
}

 

OUTPUT

No of gears are 3
Speed of bicycle is 100
seat height is 25

Types of Inheritance

Single Inheritance

Single inheritance is when a subclass inherits from one superclass. In the example below, Class A is the superclass of subclass B.

import java.io.*;
import java.lang.*;
import java.util.*;
class one
{
public void print()
{
System.out.println (“Hello World”);
}
}
class two extends one
{
public void print_for()
{
System.out.println (“for”);
}
public class Main
{
public static void main (String args [])
{
two g = new two ();
g.print();
g.print_for();
g.print();
}
}

 

OUTPUT
Hello World
for
Hello World

Multilevel Inheritance

Java does not allow multiple inheritances that is the feature of one subclass having more than one superclasses. However, Java does allow multilevel inheritance where one class can be inherited by another class, which can then inherit another.

Sample Code

import java.io.*;
import java.lang.*;
imprt java.util.*;
class one
public void print()
{
System.out.println (“Hello”);
}
}
class two extends one
{
public void print_for ()
{
System.out.println (“for”);
}
class three extends two
{
public void print()
{
System.out.println (“World”);
}
}
public class Main
{
public static void main (String args [])
{
three g = new three ();
g.print();
g.print_for();
g.print ();
}
}

 

OUTPUT

Hello
For
World

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author