Java Inheritance

Java

Inheritance in Java

Inheritance

Inheritance is a process of one class acquiring the properties of another class.
Inheritance represents the IS-A relationship which also known as a parent-child relationship.
A class that shares the properties can be referred to as superclass or base class or parent class.

The "extends" keyword indicates you are making a new class that derives from an existing class.

Subclass/child class: 

subclass is a class that derives from another class. A subclass inherits state and behavior from all of its superclasses.  

Superclass/Parent class:

Superclass is the class from where subclass inherits features. It is also called a base class or parent class.

Why Inheritance in java? 

  • For method overriding (run time polymorphism can be achieved).
  • For code reusability.

package ardousgeek.com;
public class Vehicle 
{
public String engine;
public int wheels;
public int seats;
public int fuelTank;
public String lights;
}
package ardousgeek.com;
public class Bike extends Vehicle 
{
public String handle;
}
package ardousgeek.com;
public class Car extends Vehicle
{
public String steering;
public String musicSystem;
public String airConditioner;
public String fridge;
public String entertainmentSystem;
}
package ardousgeek.com;
public class Truck extends Vehicle
{
public String steering;
public String musicSystem;
public String airConditioner;
public int container;
}
package ardousgeek.com;
public class Main 
{
public static void main(String[] args)
{
Car car = new Car();
car.steering = "Round";
car.engine = "Diesel";
System.out.println(car.engine);
}
}
Output:
Diesel
package ardousgeek.com;
public class Vehicle 
{
private String engine;
private int wheels;
private int seats;
private int fuelTank;
private String lights;
public Vehicle() 
{
this.engine = "Diesel";
this.wheels = 4;
this.seats = 6;
this.fuelTank = 40;
this.lights = "LED";
}
public Vehicle(String engne, int wheels, int seats, int fuelTank, String lights) 
{
this.engine = engine;
this.wheels = wheels;
this.seats = seats;
this.fuelTank = fuelTank;
this.lights = lights;
}
public String getEngine() 
{
return engine;
}
public int getWheels()
{
return wheels;
}
public int getSeats() 
{
return seats;
}
public int getFuelTank() 
{
return fuelTank;
}
public String getLights() 
{
return lights;
}
}
package ardousgeek.com;
public class Bike extends Vehicle 
{
private String handle;
}
package ardousgeek.com;
public class Car extends Vehicle
{
private String steering;
private String musicSystem;
private String airConditioner;
private String fridge;
public String entertainmentSystem;
public Car(String steering, String musicSystem, String airConditioner, String fridge, String entertainmentSystem)
{
this.steering = steering;
this.musicSystem = musicSystem;
this.airConditioner = airConditioner;
this.fridge = fridge;
this.entertainmentSystem = entertainmentSystem;
}
public String getSteering() 
{
return steering;
}
public String getMusicSystem() 
{
return musicSystem;
}
public String getAirConditioner() 
        {
return airConditioner;
}
public String getFridge() 
        {
return fridge;
}
public String getEntertainmentSystem() {
return entertainmentSystem;
}
public String toString() 
{
return "Car [steerng=" + steering + ", musicS=" + musicSystem + ", airCondnr=" + airConditioner
+ ", frdge=" + fridge + ", entertainSystem=" + entertainmentSystem + "]";
}
}
package ardousgeek.com;
public class Truck extends Vehicle
{
private String steering;
private String musicSystem;
private String airConditioner;
private int container;
}
package ardousgeek.com;
public class Main 
{
public static void main(String[] args) 
{
        Car car = new Car("Automatic","Boat","Samsung","HCl","onida");
        System.out.println(car);
}
}
Output:
Car [steerng=Automatic, musicS=Boat, airCondnr=Samsung, frdge=HCl, entertainSystem=onida]