Home > Tutorial > Java Inheritance Sample Code

Java Inheritance Sample Code

If your new to Java programming or haven’t had a chance to understand inheritance, let’s get that out of the way now. Inheritance in Java is allows you to build code on top of code that already exists. If you have a base class that includes some of the functionality you are working on, it’s easier to inherit the base class and build a subclass upon that. Like the old saying goes among Java programmer…why reinvent the wheel?

The following sample code give s simple example of this concept. You have a class called Vehicle, which represents the base class. The base class includes two members, vehicleName and vehicleModel. It also includes methods to set and get the two defined members. This base class is very generic, which is the point.

//Define the base class and base attributes
class Vehicle 
{
	private String vehicleMake;
	private String vehicleModel;

	
	//Constructor
	public Vehicle(String make, String model)
	{
		this.vehicleMake = make;
		this.vehicleModel = model;
	}
	
	//Set the name of the vehicle
	public void setMake(String make) 
	{
		this.vehicleMake = make;
	}
	
	//Set the model of the vehicle
	public void setModel(String model) 
	{
		this.vehicleModel = model;
	}
	
	//Get the name of the vehicle
	public String getMake() 
	{
		return(this.vehicleMake);
	}
	
	//Get the model of the vehicle
	public String getModel() 
	{
		return(this.vehicleModel);
	}
	
}


Let’s assume you have to get a little more specific and create a new Java class to define a gas powered vehicle. It makes no sense to start from scratch when you can inherit the attributes of the base vehicle class. The Java class GasVehicle extends or inherits from the base class Vehicle. The make and model members have already been done in the base class so all we need to do in this subclass is create a member for octane and the get and set that go with that.

//Define sub class for gas powered vehicles
class GasVehicle extends Vehicle
{
	
	private int minimumOctane;
	
	//Constructor
	public GasVehicle(String make, String model) 
	{
		super(make, model);
	}
	
	//Set the minimum octane
	public void setMinOctane(int octane)
	{
		this.minimumOctane = octane;
	}
	
	//Get the minimum octane
	public int getOctane()
	{	
		return(this.minimumOctane);
	}
	
}


You noticed the call to super() in each subclass constuctor. This is one of the requirements of inheritance. The call to super() is really calling the constructor of the base class. The base class constructor takes the vehicle make and model, which is why each subclass constructor has super(make, model). If you don’t call super() in the subclass, the Java compiler will give you an error.

Every time you need to add a new type of vehicle, just create a new subclass based on the base class. The sample code below creates yet another subclass for electric powered vehicles. We create a new member called batteryLife that is specific to electric vehicles. Just like before, we also include the get and set methods for batteryLife.

//Define sub class for electric powered vehicles
class ElectricVehicle extends Vehicle
{
	
	private int batteryLife;
	
	//Constructor
	public ElectricVehicle(String make, String model) 
	{
		super(make, model);
	}
	
	//Set the battery life
	public void setBatteryLife(int battery)
	{
		this.batteryLife = battery;
	}
	
	//Get the battery life
	public int getBatteryLife()
	{
		return(this.batteryLife);
	}
	
}



Sample Java Code Implements Inheritance:

package com.jcd.java.tutorials;

public class JavaInheritance 
{

	public static void main(String[] args) 
	{

		//Create a new gas vehicle object for my Ford Fusion
		GasVehicle gasVehicle = new GasVehicle("Ford", "Fusion");
	
		//Set the minimum octane rating for this gas vehicle
		gasVehicle.setMinOctane(91);
		
		//Print out make 
		System.out.println("\nGas Powered Get Model:"+gasVehicle.getMake());
		System.out.println("Gas Powered Get Make:"+gasVehicle.getModel());
		System.out.println("Gas Powered Get Make:"+gasVehicle.getOctane());
		
		//Create a new electric vehicl object for my Totoya Prius
		ElectricVehicle electricVehicle = new ElectricVehicle("Toyota", "Prius");
		
		//Set the batter life for this electric vehicle
		electricVehicle.setBatteryLife(10000);
		
		System.out.println("\nElectric Powered Get Make:"+electricVehicle.getMake());
		System.out.println("Electric Powered Get Model:"+electricVehicle.getModel());
		System.out.println("Electric Powered Get Model:"+electricVehicle.getBatteryLife());

	}

}



Sample Java Code Inheritance Output:

Gas Powered Get Model:Ford
Gas Powered Get Make:Fusion
Gas Powered Get Make:91

Electric Powered Get Make:Toyota
Electric Powered Get Model:Prius
Electric Powered Get Model:10000



Java Inheritance Notes:

  • A subclass inherits all members from its base class.
  • Constructors are not inherited by sub classes.
  • A subclass does not inherit the private members of its base class.
  • A call to super() must be made as the first call within the constructor of a subclass.
Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot