Skip to content
Code To Live By » Java Tutorial » Inheritance in Java

Inheritance in Java

  • by

Inheritance is one of the core pillars of Object Oriented Programming. A class can “inherit” attributes and methods from another, allowing you to create hierarchies of classes which can reuse common code.

What might this look like? We’ll step away from the Ice Cream / Desserts that we have been using for the past couple pages. Let’s say that we wanted to talk about musical instruments, we might have the following base class for stringed instruments.

public class StringedInstrument {
	
	String tuning;
	
	public StringedInstrument(String tuningParameter) {
		tuning = tuningParameter;
	}
	
	public void playString(int stringNumber) {
		System.out.println(tuning.charAt(stringNumber));
	}
	
	public void tune(String newTuning) {
		tuning = newTuning;
	}
}

For simplicity’s sake we will represent the tuning of our instrument as a String data type containing the values of each (musical) string. So a guitar in standard tuning would be EADGBE. Again, for simplicity’s sake we can have a playString method which will play the open string in our current tuning given a string number.

But let’s say we wanted some slight changes to account for specific instruments. The implementations of these methods would be the same, so we could inherit them from StringedInstrument as a base class.

If we wanted to make some subclasses for some different types of instruments that might look something like this:

public class Cello extends StringedInstrument {
	
	public Cello() {
		super("CGDA");
	}

}
public class Guitar extends StringedInstrument {
	
	public Guitar() {
		super("EADGBE");
	}

}
public class Violin extends StringedInstrument {

	public Violin() {
		super("GDAE");
	}

}

Each of our subclasses here “extends StringedInstrument” in its class declaration. Constructors are not inherited, so we do need a new constructor for each of our subclassses, but we are able to call the constructor of the base class by using the keyword “super.”

What if we wanted to go even a step further? Guitars can be acoustic or electric. Many of the settings for an electric guitar would be controlled by an amp but we would likely have a knob for the volume as well as the gain on the guitar itself.

public class ElectricGuitar extends Guitar {
	
	private int volume;
	private int gain;
	
	public ElectricGuitar(int volumeParameter, int gainParameter) {
		super();
		volume = volumeParameter;
		gain = gainParameter;
	}
	
	@Override
	public void playString(int stringNumber) {
		if (gain >= 5) {
			System.out.print("~distorted~");
		}
		super.playString(stringNumber);
	}

}

We can also override our playString method to include our new gain attribute, while also keeping the functionality from the base class implementation through use of the super keyword.

Multiple Inheritance

Something else to note here is that Java does not support the concept of “Multiple Inheritance.” You can, however, implement multiple interfaces, or even inherit a class as well as implementing interfaces.

Now you have a good idea of the conpept of Inheritance in Java!

Leave a Reply

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