Inheritance In Java -10 Program

--help cse
5 min readMar 20, 2022

All concepts of inheritance in java is covered.

Inheritance In Java

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs

(Object Oriented programming system).

Terms used in Inheritance

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

The syntax of Java Inheritance

class Subclass-name extends Superclass-name

{

//methods and fields

}

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.

Program 1:Simple inheritance Example with extends keywords

Code:

class Employee{

float salary=40000;

}

class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println(“Programmer salary is:”+p.salary);

System.out.println(“Bonus of Programmer is:”+p.bonus);

}

}

Output:

Programmer salary is:40000.0

Bonus of programmer is:10000

In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.

Types of inheritance in java

We will see all in details:

Single Inheritance Example

When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Program 2:Single Inheritance Level example

CODE:

class Animal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

void bark(){System.out.println(“barking…”);}

}

class TestInheritance{

public static void main(String args[]){

Dog d=new Dog();

d.bark();

d.eat();

}}

OUTPUT:

barking...eating...

Program 3: Single Inheritance Level example

CODE:

// Java program to illustrate the

// concept of single inheritance

import java.io.*;

import java.lang.*;

import java.util.*;

class one {

public void print_name()

{

System.out.println(“Sachin”);

}

}

class two extends one {

public void print_for() { System.out.println(“bat”); }

}

// Driver class

public class Main {

public static void main(String[] args)

{

two g = new two();

g.print_name();

g.print_for();

g.print_name();

}

}

Output:

Sachin

bat

Sachin

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

Program 4:Multilevel Inheritance Level exampleFilename:multilevel.javaCODE:

class Animal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

void bark(){System.out.println(“barking…”);}

}

class BabyDog extends Dog{

void weep(){System.out.println(“weeping…”);}

}

class multilevel{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}}OUTPUT:weeping...barking...eating...Program 5:Multilevel Inheritacen exampleCODE:

// Java program to illustrate the

// concept of Multilevel inheritance

import java.io.*;

import java.lang.*;

import java.util.*;

class one {

public void print_geek()

{

System.out.println(“Sachin”);

}

}

class two extends one {

public void print_for() { System.out.println(“bat”); }

}

class three extends two {

public void print_geek()

{

System.out.println(“fame”);

}

}

// Drived class

public class Main {

public static void main(String[] args)

{

three g = new three();

g.print_geek();

g.print_for();

g.print_geek();

}

}

OUTPUT:

fame

bat

fame

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

Program 6: Hierarchical Inheritance Example

Filename: Hirarchial.java

Code:

class Animal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

void bark(){System.out.println(“barking…”);}

}

class Cat extends Animal{

void meow(){System.out.println(“meowing…”);}

}

class Hirarchial{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

//c.bark();//C.T.Error

}}

Output:

meowing...eating...

Why multiple inheritance is not supported in java?

We will see through program

Program 7:Multiple inheritance example

Code:

class A{

void msg(){System.out.println(“Hello”);}

}

class B{

void msg(){System.out.println(“Welcome”);}

}

class C extends A,B{//suppose if it were

public static void main(String args[]){

C obj=new C();

obj.msg();//Now which msg() method would be invoked?

}

}

Output:

Compile Time Error

Multiple Inheritance (Through Interfaces): In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interface A and B.

Program 8: Multiple inheritance example

Code:

// Java program to illustrate the

// concept of Multiple inheritance

import java.io.*;

import java.lang.*;

import java.util.*;

interface one {

public void print_name ();

}

interface two {

public void print_for();

}

interface three extends one, two {

public void print_name ();

}

class child implements three {

@Override public void print_geek()

{

System.out.println(“Sachin”);

}

public void print_for() { System.out.println(“for”); }

}

// Drived class

public class Main {

public static void main(String[] args)

{

child c = new child();

c.print_name();

c.print_for();

c.print_name();

}

}

Output:

SachinforSachin

Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Program 9: Hybrid Inheritance example

Code:

class SolarSystem {

}

class Earth extends SolarSystem {

}

class Mars extends SolarSystem {

}

public class Moon extends Earth {

public static void main(String args[])

{

SolarSystem s = new SolarSystem();

Earth e = new Earth();

Mars m = new Mars();

System.out.println(s instanceof SolarSystem);

System.out.println(e instanceof Earth);

System.out.println(m instanceof SolarSystem);

}

Output:

truetruetrue

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

Program 10:Inheritance example of identifying speed of bicycle and gear through inheritance in java.

Code:

// 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);

}

}

// 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);

}

}

// driver class

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 3speed of bicycle is 100seat height is 25Thank you.I hope Inheritance in java and its type clear.

--

--