permaculture playing cards*
The moose likes Beginning Java and the fly likes Dynamic Binding of methods and data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic Binding of methods and data" Watch "Dynamic Binding of methods and data" New topic
Author

Dynamic Binding of methods and data

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
Mainexe1: A Subclass object sub4b is created an the printone() method is called. As printone() is not overwritten in Subclass the Superclass printone() methode is called with the local value a
= 4 from Superclass.
In Mainexe1a it is the same but there the value of a is retrieved
by a geta()-Methode which is overriden in subclass Sub4b.
It seems that the JVM realizes it is a sub4b object and calls
methods from Sub4b - if they exist.
As printone() only exists in Superclass it calls it from Super
class with value of a from Superclass. But a is not accessed
directly but by a Method geta() which is overwritten in Subclass
it calls Subclass again (s is Subclass Object). As we are now in
Subclass with geta() again it takes Subclass value of a (a=4)
sinstead of Supercalls value of a=3.
DOES ANYBODY HAVE A BETTER DOCUMENTATION/EXPLANATION FOR THIS COMPLEXE POLYMORPHISTIC BEHAVOUR?
Appreciate your answers.

Thomas
================================================================
class Sub4a
{
int a=3; //***************************
Sub4a() { System.out.println("Inside Constructor Sub4a "+a); } // Constructor super
public void printone() {System.out.println("Inside Method printone() "+a);}
}
class Sub4b extends Sub4a
{
int a=4;
Sub4b() { System.out.println("Inside Constructor Sub4b "+a); } // Constructor sub
}
public class Mainexe1 //Main programm Mainexe1.java
{
public static void main(String arg[])
{
Sub4b s = new Sub4b();
s.printone();
}
}

Result:
Inside Constructor Sub4a 3
Inside Constructor Sub4b 4
Inside Method printone() 3 //Why?////!!!
class Sub4a
{
int a=3; //***************************
Sub4a() { System.out.println("Inside Constructor Sub4a "+a); } // Constructor super
public void printone() {System.out.println("Inside Method printone() "+geta());}
public int geta() {return a;}
}
class Sub4b extends Sub4a
{
int a=4;
Sub4b() { System.out.println("Inside Constructor Sub4b "+a); } // Constructor sub
public int geta() {return a;}
}
public class Mainexe1a //Main programm Mainexe1a.java
{
public static void main(String arg[])
{
Sub4b s = new Sub4b();
s.printone();
}
}
Result:
Inside Constructor Sub4a 3
Inside Constructor Sub4b 4
Inside Method printone() 4 //Why?////!!!


------------------
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
The difference is that methods can be polymorphic but data members aren't.
In the first case printone is in the base class and refers to data member a. So even when you call printone from a derived class you are calling it in the base class. The base class only knows its value of a.
In the second case printone calls geta. You have overridden geta in the derived class and it returns the derived class value of a.
I'm prety sure the Java Language Spec talks about this, just can't remember the section.
John


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Binding of methods and data