DEV Community

Rajan S
Rajan S

Posted on

Inheritance of java &types

Java Inheritance is a fundamental concept in 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. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class

types of inheritance

Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
Enter fullscreen mode Exit fullscreen mode
  1. Single Inheritance

class vehicle{

void bike()
{
    System.out.println("bike comfort for two person");
}
Enter fullscreen mode Exit fullscreen mode

}

class car extends vehicle {

void red()
{
    System.out.println("car comfort for four person");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Single {
public static void main(String args[]) {

car c1 = new car ();

c1.bike();
c1.red();

}
Enter fullscreen mode Exit fullscreen mode

}

Output

bike comfort for two person
car comfort for four person

  1. Multilevel Inheritance

class vehicle{

void bike()
{
    System.out.println("bike comfort for two person");
}
Enter fullscreen mode Exit fullscreen mode

}

class car extends vehicle {

void red()
{
    System.out.println("car comfort for four person");
}
Enter fullscreen mode Exit fullscreen mode

}

class bicycle extends car {

void black ()
{
    System.out.println("i like so much");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Multilevel {

public static void main(String args[]) {

bicycle b1 = new bicycle();

b1.bike();
b1.red();
b1.black();

}
Enter fullscreen mode Exit fullscreen mode

}

output

bike comfort for two person
car comfort for four person
i like so much

  1. Hierarchical Inheritance

class vehicle{

void bike()
{
    System.out.println("bike comfort for two person");
}
Enter fullscreen mode Exit fullscreen mode

}

class car extends vehicle {

void red()
{
    System.out.println("car comfort for four person");
}
Enter fullscreen mode Exit fullscreen mode

}

class van extends car {

void white ()
{
    System.out.println("van comfort in more person");
}
Enter fullscreen mode Exit fullscreen mode

}

public class Hierarchical {

public static void main(String args[]) {

van v1 = new van();

v1.bike();
v1.red();
v1.white();

}
Enter fullscreen mode Exit fullscreen mode

}

output

bike comfort for two person
car comfort for four person
van comfort in more person

Top comments (0)