Results 1 to 6 of 6
- 01-26-2011, 05:50 PM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
super class reference variable accesses overriding sub class method
My Super class is shown below
My SubClass extends the above Super class and it is shown belowJava Code:class Super { Super () { this.superMethod(); } void superMethod() { System.out.print(new Integer(8)); } void anotherSuperMethod() { System.out.print(new Integer(7)); } }
My main method is shown belowJava Code:class SubClass extends Super { SubClass () { this.superMethod(); } void superMethod() { [I][COLOR="Green"]//overriding[/COLOR][/I] System.out.print(new Integer(1)); } void anotherSubMethod() { System.out.print(new Integer(9)); } }
As said in the comments obj2.superMethod() goes to the method in SubClass and not Super class.Java Code:public static void main(String[] args) { SubClass obj1 = new SubClass(); obj1.superMethod(); //as expected : calls superMethod in SubClass Super obj2 = new Super(); obj2.superMethod(); //as expected : calls superMethod in Super obj2 = obj1; //I think it is similar to Super obj2 = new SubClass(); System.out.println("**************"); obj2.superMethod(); //calls superMethod in SubClass. Why? obj2.anotherSuperMethod(); //as expected : calls anotherSuperMethod in Super obj2.anotherSubMethod(); //compiler error }
If it can do it, why cant it access another "ordinary" method in SubClass (obj2.anotherSubMethod())
- 01-26-2011, 05:58 PM #2
Because the compiler doesn't "know" that obj2 is a SubClass. The reason it calls SubClass's overridden superMethod is that the compiler doesn't have to "know" which version of the method is being called, as long as it's guaranteed to be there.
You could tell the compiler about it via casting:
Java Code:((SubClass)obj2).anotherSubMethod();
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
You can, but you have to cast it since the variable is a Super variable, and that's all the compiler knows.
Java Code:((SubClass)obj2).anotherSubMethod();
edit: as Kevin well states above! :)
- 01-26-2011, 06:13 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
I understood the below quote
This is the reason why compiler error occurs when obj2 is trying to access anotherSubMethod()Because the compiler doesn't "know" that obj2 is a SubClass
But, sorry to trouble you guys again, could someone please elaboratewhy can't it go to superMethod() in Super class:confused:compiler doesn't have to "know" which version of the method is being called
-
This is one of the cornerstones of object oriented programming, method overriding, polymorphism, or calling virtual methods, and any basic text or tutorial (or likely Kevins' answer) will likely explain it better than I can. For details, please see the Wikipedia article on virtual methods. This is one reason why I believe it is considered possibly dangerous to call over-ridable methods in an object's constructor -- unless you really know what you're doing and how this can effect behavior.
Last edited by Fubarable; 01-26-2011 at 06:19 PM.
- 01-26-2011, 06:38 PM #6
Think about it this way: say you have an Animal class, and a default eat() function:
But then you want more specific versions of Animals, so you extend Animal:Java Code:public class Animal{ public void eat(){ System.out.println("An animal is eating."); } }
Then somewhere else, you have a List populated with instances of Animal:Java Code:public class Cat extends Animal{ public void eat(){ System.out.println("A cat is eating. Meow!"); } } public class Mouse extends Animal{ public void eat(){ System.out.println("A mouse is eating. Squeak!"); } }
And you want to make all of the Animals eat:Java Code:List<Animal> animals = new ArrayList<Animal>(); animals.add(new Animal()); animals.add(new Cat()); animals.add(new Mouse());
The beauty is that you don't have to know ahead of time what kind of Animal each instance is going to be. Each one will call the correct eat method: the "lowest" version of the overriden method. Try throwing together a similar example and see what happens.Java Code:for(Animal a : animals){ a.eat(); }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
how to call higher level super class method?
By satheeshtech in forum Advanced JavaReplies: 2Last Post: 01-12-2010, 03:11 PM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 05:33 PM -
Passing Class Reference to method
By nekt in forum Advanced JavaReplies: 5Last Post: 03-26-2009, 05:08 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
[SOLVED] Using classes and overriding one class for another
By StealthRT in forum New To JavaReplies: 3Last Post: 04-08-2008, 07:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks