Results 1 to 6 of 6
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Question super class reference variable accesses overriding sub class method

    My Super class is shown below
    Java Code:
    class Super {
    	Super () {
    		this.superMethod();
    	}
    	void superMethod() {
    		System.out.print(new Integer(8));
    	}
    	void anotherSuperMethod() {
    		System.out.print(new Integer(7));
    	}
    }
    My SubClass extends the above Super class and it is shown below
    Java 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));
    	}
    }
    My main method is shown below

    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
    	}
    As said in the comments obj2.superMethod() goes to the method in SubClass and not Super class.
    If it can do it, why cant it access another "ordinary" method in SubClass (obj2.anotherSubMethod())

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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! :)

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    I understood the below quote
    Because the compiler doesn't "know" that obj2 is a SubClass
    This is the reason why compiler error occurs when obj2 is trying to access anotherSubMethod()

    But, sorry to trouble you guys again, could someone please elaborate
    compiler doesn't have to "know" which version of the method is being called
    why can't it go to superMethod() in Super class:confused:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by subith86 View Post
    But, sorry to trouble you guys again, could someone please elaborate
    why can't it go to superMethod() in Super class:confused:
    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.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Think about it this way: say you have an Animal class, and a default eat() function:

    Java Code:
    public class Animal{
       public void eat(){
          System.out.println("An animal is eating.");
       }
    }
    But then you want more specific versions of Animals, so you extend 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!");
       }
    }
    Then somewhere else, you have a List populated with instances of Animal:

    Java Code:
    List<Animal> animals = new ArrayList<Animal>();
    animals.add(new Animal());
    animals.add(new Cat());
    animals.add(new Mouse());
    And you want to make all of the Animals eat:

    Java Code:
    for(Animal a : animals){
       a.eat();
    }
    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.
    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

  1. how to call higher level super class method?
    By satheeshtech in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2010, 03:11 PM
  2. Replies: 20
    Last Post: 06-03-2009, 05:33 PM
  3. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 05:08 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  5. Replies: 3
    Last Post: 04-08-2008, 07:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •