Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Binding of methods and data

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic