Posts tagged ‘Java Inheritance’

Beginners Java Tutorial – Inheritance

We also have some excellent Beginners Programming Java tutorials that use high quality narrated videos and practical working files to teach the fundamentals of programming in Java Beginners Java Training Videos – Programming


Beginners Java Tutorial – Introducing Inheritance

Programming can inevitably be a time-consuming and laborious process; for this reason programmers always look for ways to improve efficiency, such as the re-use of existing code. The structure of the Java language facilitates the practice of re-using code in a number of ways. One of these is Inheritance, which can have a profound effect on the way that you design your projects as a whole.

The basic principle in Inheritance is that you can create a Class that re-uses the code in an already existing Class by extending (inheriting from) it. The new Class inherits all of the features of the original Class, and is free to either add additional features or alter the features it has inherited, such as the way in which a method is implemented.

When a Class inherits from another Class, it is said to ‘extend’ it. The original Class is referred to as the Superclass, the inheriting Class being the Subclass. All Classes in the Java language ultimately extend the Class Object, although you do not state this explicitly in your Class declarations. This is why any Class that you create will naturally provide the methods outlined in the Object Class, such as toString(), even when you didn’t declare those methods in your Class declaration.

Let’s create a simple example – in your IDE create a new Java project and Main Class (leave the main method empty for now). Create a second Class called MySuperClass and enter the following code:

/**A Superclass ..
public class MySuperClass {

	//instance variables

	/**an int */
	private int someInt;
	/**a String */
	private String someString;

	 * Constructor function
	 * @param intValue
	 * @param stringValue
	public MySuperClass(int intValue, String stringValue)

	 * Method outputs an arbitrary string
	public void aGreatMethod()
		System.out.println("This really is a great method");

Now create another Class called MySubClass and enter the following:

 * MySubClass inherits from MySuperClass
 * and provides one additional method
 * - This class will provide methods and
 *   variables present in the SuperClass
 *   although they are not declared here
public class MySubClass extends MySuperClass {

	 * Constructor function conforms to the
	 * superclass constructor
	 * @param subIntValue
	 * @param subStringValue
	public MySubClass(int subIntValue, String subStringValue)
			/* call the superclass constructor
			 * which expects an int and a string
		super(subIntValue, subStringValue);

	 * Additional method provided in MySubClass
	public void anotherMethod()
		System.out.println("This method is pretty good");

Now, return to your Main class and enter the following as your main method:

public static void main(String[] args)
		//create an instance of the subclass
	MySubClass mySub = new MySubClass(99, "blah");
		//call the subclass' own method
		//call the inherited method


When you run the program you should see the following written to standard output:

This method is pretty good
This really is a great method

As you can see from the above code, all you need to do to extend a Class is to use the syntax ‘extends’ in your Class declaration and ‘super()’ in your constructor method to call the constructor for the Superclass. As well as extending your own classes (as above) you can extend Classes that already exist, either within the language itself or within any external libraries that you happen to be using.

One common reason for using inheritance is to provide Classes that ‘specialise’ in some way. For example, you might have a program in which you want to represent the different sets of users of an application. These users might have some things in common but some differences – instead of having several distinct Classes in which you use sections of similar code, you could create a Superclass (e.g. User) and have several Subclasses inheriting from it. Your Superclass would provide all of the characteristics that the users have in common, while each Subclass can focus on what distinguishes it from the others.

Additional guidelines for using Inheritance:

  • Each Subclass can only have one Superclass, although a Superclass can have many Subclasses.
  • You can provide an alternative implementation for any method that you inherit from a Superclass by ‘overriding’ it – to do this you simply provide the method within your Class declaration and, providing the method signature is the same (same method name, parameter types/ number, and return type), your implementation will be the one that is called on objects of your Class.

Advanced Java topics relating to this Java tutorial are Polymorphism, Interfaces and Abstract Classes.


December 11, 2008 at 2:04 pm Leave a comment