wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Dynamic Method Dispatch, What happens at JVM level Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic Method Dispatch, What happens at JVM level" Watch "Dynamic Method Dispatch, What happens at JVM level" New topic
Author

Dynamic Method Dispatch, What happens at JVM level

Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi
If i have a attribute both in sub and super class
superClass obj = new subClass();
obj.callme();
call callme() present in subclass
where as
System.out.println(obj.counter); prints value assigned in super class

whyyyyyyyy???


I want to know what happens at JVM level.
Is it coz attribute is attached outside Method Area in JVM and is attached to class declaration

plz Help





class testInheritance
{
public static void main (String s[])
{ superClass obj = new subClass();
obj.callme();
System.out.println(obj.counter);
}

}

class superClass
{ int counter = 0;
void callme()
{System.out.println("Super class");
}
}


class subClass extends superClass
{ int counter = 10;
void callme()
{System.out.println("Sub class");
}
}


bash-3.00$ java testInheritance
Sub class
0


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The simple answer is just "because that's the way it is". Fields are not polymorphic. If you define a field in a subclass that has the same name as a field in a superclass, it is said to "hide" the superclass field, and all sorts of bad, confusing things happen. There's no reason to do this; the subclass can simply assign a new value to the existing superclass field.

This is hardly an advanced topic; moving to Java in General (Beginner) for any further discussion.


[Jess in Action][AskingGoodQuestions]
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199

The simple answer is just "because that's the way it is". Fields are not polymorphic. If you define a field in a subclass that has the same name as a field in a superclass, it is said to "hide" the superclass field, and all sorts of bad, confusing things happen. There's no reason to do this; the subclass can simply assign a new value to the existing superclass field.


if field in subclass hides field in superclass then why
System.out.println(obj.counter);
prints value assigned in super class
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Because "obj" is a reference to the superclass type. If println(obj.counter) printed the subclass value, then that would mean the variable was being chosen based on the runtime type of obj; but it's not, it's being chosen based on the compile-time type. In other words, as I said, fields are not polymorphic. The "hiding" occurs only if you have a reference of subclass type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Method Dispatch, What happens at JVM level
 
Similar Threads
Private method
The method output
hide vs override
Dynamic dispatch
Dynamic Method Dispatch, What happens at JVM level