Method shadowing (Inheritance)

18 November 2007

Method shadowing is a term used for situations, when child and parent classes have methods with exactly same signature. This situation can create problems that sometimes become difficult to understand.

Method shadowing can be done on purpose or by mistake.

Consider the example below:

public class Car {
 
     	public void startCar()
	{
		System.out.println("Car class - Car started");
	}
 
	public void stopCar()
	{
		System.out.println("Car class - Car stopped");
	}
 
        public void turnOnSpecialFeatures()
        {
               System.out.println("Car class - Special features are now running.");	
        }
 
}
 
public class Mazda extends Car {
 
        public void turnOnSpecialFeatures()
       {
                System.out.println("Mazda class - Special features are now running.");	
       }
 
}
 
Public class MainClass {
 
 
	public static void main(String[] args) {
		Mazda obj = new Mazda();
		obj.startCar();
		obj.turnOnSpecialFeatures();
		obj.stopCar();
 
	}
 
}

Output:

Car class - Car started
Mazda class - Special features are now running.
Car class - Car stopped

Method named turnOnSpecialFeatures exists in class Car and also in class Mazda. The output suggests that the turnOnSpecialFeatures method defined in class Mazda has executed. Actually, methods in child class get preference over methods defined in the parent class. When a method is called, Java compiler searches for the method in the child class. If not found, then the method is searched in the parent class.

Now suppose, we really want to call the method turnOnSpecialFeatures of the parent class. For that we will add the following method in the Mazda class.

public void turnOnSpecialFeaturesCar()
{
     super.turnOnSpecialFeatures();
}

Now when we call turnOnSpecialFeatureCar method, turnOnSpecialFeatures of Car class will be called.

It’s a little thing but can cause problems that are hard to understand.

Happy coding.

del.icio.us:Method shadowing (Inheritance)  digg:Method shadowing (Inheritance)  spurl:Method shadowing (Inheritance)  wists:Method shadowing (Inheritance)  simpy:Method shadowing (Inheritance)  newsvine:Method shadowing (Inheritance)  blinklist:Method shadowing (Inheritance)  furl:Method shadowing (Inheritance)  reddit:Method shadowing (Inheritance)  fark:Method shadowing (Inheritance)  blogmarks:Method shadowing (Inheritance)  Y!:Method shadowing (Inheritance)  smarking:Method shadowing (Inheritance)  magnolia:Method shadowing (Inheritance)  segnalo:Method shadowing (Inheritance)  gifttagging:Method shadowing (Inheritance)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Method shadowing (Inheritance)

4 Responses to “Method shadowing (Inheritance)”

  1. afsina Says:

    that is why in the example above, you should make CAR an interface.. second, i am not sure why this causes a problem, this totaly the expected and intended behavior.

  2. WebDynamics Says:

    Thanks for your comment.
    Making CAR an interface is a nice idea… but I am assuming that startCar and stopCar are same for all the cars. So no need to interface in that case.

    Also consider the example for “Method shadowing”. I know its not the ideal way of doing it .. but to show Method shadowing .. I have written this example.

  3. Techno Modus Says:

    Concept-oriented programming (CoP) provides dual methods for that purpose. In particular, you can use them either to override child methods from parent class or to override (as usual) parent methods from child classes (white paper: http://conceptoriented.com/papers/CopInformalIntroduction.html). Also the similar possibility is provided by so called inner methods (for example, in the Beta programming language).

  4. Nayyer Says:

    In object oriented programming and design we call it Overriding and this is certainly not Shadowing and the behaviour(Polymorphism) you described is what is intended. In java one can shadow the supper class properties in derived classes but Java does not support method shadowing. Any declaration of method with same signature in derived classes will always work as overriden method.

Leave a Reply