Is there any way to access an overridden method of super class?

class Animals
     void makeNoice(){System.out.println("General noice");}
class Dog extends Animals
          void makeNoice(){System.out.println("bark");}
};Is there any way to access the makeNoice() of Animals class from a Dog object
Dog dog = new Dog();
Animal animaldog = dog;
animaldog.makeNoice(); // will always calls Dog's makeNoice() ie the overriden method.
Is there a way to access the Animals makeNoice() method ? by using cast or super etc?
Or it is not possible at all?

Rajeebs wrote:
Now another question coming in my mind.
Whether any way to access a method which belong to super class's super class without using any method of class B,
like super.super.fn() ??Isn't this just the same question again? And won't you again just "solve" it by writing some invokeSuperSuperMethod or other? This flawed design is quickly getting out of hand, isn't it? The question isn't "How can I invoke an arbitrarily deep superclass' method?" but more "Why do I need to invoke a method belonging to a concrete type at all?". If you need to do that, chances are you've misused inheritance.
To use your initial sample code, in what circumstance would you require that a Dog make anything other than a Dog noise? It makes no sense. Point is, you have an Animal abstraction, and can call makeNoise on any instance of any subclass, and it will make the appropriate noise. By trying to use trickery to force other noises, you're doing something unnatural, and you're also depending on actual specific concrete subclasses. If you're going to go to specific classes and ask them to make their noise, what use is the inheritance? What use it the polymorphism?
Are we heading into another "I've got a brilliant idea for a [useless language feature|http://forums.sun.com/thread.jspa?threadID=5423706&messageID=10905772#10905772], guys!" thread?

Similar Messages

Maybe you are looking for