Prev Next

Overriding Principle

When you implement a new method in the subclass that has the same signature as the one in the superclass, then the subclass method is said to override the super class method. i.e. in the presence of sub class method with the same signature, the super class method remains hidden.

When you extend a class, at times, you may want to override a functionality implemented by the super class. This may take one of the two forms.

  1. Extend the functionality in addition what is defined in the super class.
  2. Re-implement a functionality defined in the super class.

1. Extend the functionality

In this scenario, you would typically call the super class method and add 'extra code' to carry out the 'extra work'.

As an example, lets say you want to check if a 3D point is origin. You can invoke the isOrigin() method of Point class and add an additional check to confirm if z coordinate is also 0. The code is given below.

	// To be added to Point3D class

	public boolean isOrigin() {
		if ( super.isOrigin() && this.z == 0 )
			return true;
		else
			return false;
	}

Note the way isOrigin() method of Point class is invoked from Point3D class. The keyword super is used to invoke the parent class method.

  • Point3D's isOrigin() overrides Point's isOrigin() since both have exactly the same signature, but the subclass method invokes the super class method under the cover to get part of the job done.

  • Between the two defintions of isOrigin(), which one will be invoked depends on the object instance. An example is shown below to clarify this.
  • 		Point p = new Point();			// An instance of Point
    		Point3D p3 = new Point3D();		// An instance of Point3D
    
    		p.isOrigin();	// invokes Point's isOrigin() method
    		p3.isOrigin();	// invokes Point3D's isOrigin() method
    	

2. Re-implement the functionality

You may want to redo the functionality since a straightforward extension may not be possible. Consider the toString() method of Point class which converts the object to a printable string form: (x,y). Now you would like to convert into (x,y,z) form. Although this can achieved by calling toString() of the super class and inserting a ',' and z value before the ')', such an implementation is non-intuitive and hence is not desirable. It is best to re-implement as shown.

	// In Point3D.java

	public String toString() {
		return "(" + x + "," + y + "," + z + ")";
	}

Invoking the super class constructor

We saw above that super class method can be invoked from sub class. It is also possible to invoke the constructor(s) in a similar way. This is show below.

	Point3D(int xInit, int yInit, int zInit) {
		super(xInit, yInit);
		this.z = zInit;
	}

i.e. just calling super() and passing the arguments is sufficient.

Overriding super class attributes

It is possible to hide the super class attribute by declaring an attribute with the same name in the sub class. For example, declaring an attribute x in Point3D will hide Point's x. This should be avoided unless there is a strong reason to do so.

Static methods cannot be overridden

Why? Method overriding is 'dynamic' in nature as the method being invoked depends on whether the object is super class or sub class instance. But, 'static methods' belong to the class, and accessed by class name rather than object name. If both the super and sub classes have a static method with the same signature, the sub class one is said to hide the super class one. Also, invoking the super class method by 'super' keyword does not make any sense.