Inheritance is about inheriting the properties and methods in sub classes to reuse them.
Define a class Engine
Define a class Vehicle which extends Engine class.
output will be:
Vehicle capacity is 2.5 and fuel type is Diesel
Define a class Engine
public class Engine { public static String getCapacity(){ return "2.5"; } }
public class Vehicle extends Engine { public static void main(String args[]) { System.out.println("Vehicle capacity is "+ getCapacity()+" and fuel type is "+ getFuelType()); } public static String getFuelType(){ return "Diesel"; } }We can observe main method uses methods in Engine class
output will be:
Vehicle capacity is 2.5 and fuel type is Diesel