DEV Community

pranavinu
pranavinu

Posted on • Edited on

Inheritance

Inheritance:
1)A class consist of a variables and methods and that can be accessed
by any others class by using an inheritance
2)extends keyword is to achieve inheritance
3)extends-It is to access a variables and methods in a class,which is created in another class
4)It has 5 types
*single inheritance
*multilevel inheritance
*hierarchical inheritance
*hybrid inheritance
5)It is to avoid duplication in a different class

single inheritance:
6)One child class that can access One parent class
7)Parent class-the variables and methods which is created inside the class
8)Child class-the parent class that can be accessed inside the class in
another class
9)The object which is created inside the child class can aceess both
the parent and child class

program-2

parent class

public class vehicle {

String brand;
String color;


void method1()
{
    System.out.println("gear");
}

public static void main(String[] args) {

}
Enter fullscreen mode Exit fullscreen mode

}

child class

public class car extends vehicle

{

public static void main(String[] args) {

    car object1 =new car();


    object1.color="red";
    object1.brand="bmw";

    System.out.println(object1.color);
    System.out.println(object1.brand);
    object1.method1();



}
Enter fullscreen mode Exit fullscreen mode

}

output:
red
bmw
gear

Top comments (0)