java inheritance with example

java inheritance with example

Inheritance in Java is one of the key features of Object Oriented Programming(OOPS).Inheritance is the mechanism of creating new class from existing class.which means a class can reuse the fields and methods of the existing class without writing them.

A class that is inherited from existing class is called a subclass (also a derived class, extended class, or child class).The existing class from which the subclass is inherited is called a superclass (also a base class or a parent class).

Inheritance in Java defines is-a relationthip between super class and sub class.

Inheritancein java is implemented by using extends keyword.

A subclass inherits all of the public and protected members of its Superclass(parent class),If the subclass is in the same package as its super class, it also inherits the package-private members of the Super class.

A subclass does not inherit the private members of its super class

A Sub class can inherit all properties like fields and methods but constructors are not members of a class so child class cannot inherit the parent class constructor. but super class constructor can be invoked from the subclass.See The below diagram

Syntax for Inheritance


public class Subclass extends Superclass{
// fields && methods
}

Advantages of inheritance

  1. Reuse of the existing code
  2. For Polymorphism

Now Lets See One basic Example


public class Account {
int accountNumber = 123;
}

public class SavingsAccount extends Account {
private String type = "savings";

public static void main(String[] args) {

SavingsAccount sa = new SavingsAccount();
System.out.println("accountNumber" + sa.accountNumber);
System.out.println("accountType" + sa.type);

}
}

output


Types of inheritance

  • single inheritance.
  • multilevel inheritance.
  • hierarchial inheritance.
  • hybrid inheritance.
  • multiple inheritance.

single inheritance

In single inheritance we have only one super class and only one sub class. producing sub classes from a single super class is called single inheritance.


Example:

Syntax of single Inheritance

below A is super class and B is child class extends A


public class A{
}
public class B extends A
{
}


public class Vehicle
{
public void wheels()
{
System.out.println("vehicle is having wheels");
}
}
public class Car extends Vehicle{
public void engine(){
System.out.println("car has isuzu engine");
}
public static void main(String[] args)
{
Car c = new Car();
c.engine();
c.wheels();
}
}

output


vehicle is havingwheels


car has isuzu Engine


Note:


Whenever we are using inheritance if we create child class object with child reference.so that we can access the methods belongs to super class and subclass.


If we create child class object with parent class reference ,so we can access only super class methods.


Multilevel Inheritance

In Multilevel Inheritance we have one super class and one sub class and many number of intermediate  super classes.

Here A is super class and C is sub class and B is intermediate class

Example


class A{
public void print(){
System.out.println("super class print method");}
}
class B extends A{
public void classBmethod(){
System.out.println("class B method");
}

}

public class C extends B{
public void classCmethod(){
System.out.println("class c method");
}

public static void main(String[] args){

C c = new C();
c.classCmethod();
c.classBmethod();
c.print();
}
}

output

Multiple inheritance:

if we have more than one super class and only one sub class thyen it is said to be multiple inheritance.in java does not support multiple inheritance.


class A{
public void print(){
System.out.println("super class print method");}
}
class B{
public void print(){
System.out.println("super class print method");}
}

}

public class C extends A,B{

public static void main(String[] args){

C c = new C();

c.print();
}
}

output


compiletime Error
-->