• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overloading(why the answer is b, but not c

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from Marcus tutorial:
ppen when you attempt to compile and run the following code?

1)
RType.amethod
-1
RType.amethod

2)
RType.amethod
99
RType.amethod
3)
99
RType.amethod

4)
Compile time error

[This message has been edited by Jim Yingst (edited February 08, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the subject line: "Overloading(why the answer is b, but not c"
I assume you mean, why is it 2) and not 3)? Look at the line "Base b = new RType();". The RType constructor will call the superclass constructor Base(), which calls amethod(), which is overridden in RTest so it prints "RType.amethod()".
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remembered something when i read this.
When a method is not private then it is dynamically linked. So, the method amethod() is from derived class. But as the instance varible is not dynamically linked, it is considered as the compile time type.
Is this Ok, Jim? Thanks.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got it, Thandapani. Actually it's "when a method is not private or static then it is dynamically linked". But of course the "static" part wasn't relevant in this problem. And variables are never dynamically linked, regardless of modifiers.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
I am not following why superclass constructor Base()calls amethod() from the derived class but from the Base class?
"which is overridden in RTest so it prints "RType.amethod" -- would you please clarify this further? Thanks.
regards,
mondal

Originally posted by Jim Yingst:
From the subject line: "Overloading(why the answer is b, but not c"
I assume you mean, why is it 2) and not 3)? Look at the line "Base b = new RType();". The RType constructor will call the superclass constructor Base(), which calls amethod(), which is overridden in RTest so it prints "RType.amethod()".


 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can solve this problem using a question.
Q. When base class calls a method, how could the run time knows to called the overridden method in the derived class? The object is not even constructed yet.
A:
Why? From the language theory, it is called deferred binding or dynamic binding or binding at run time. Compiler leaves the decision to run time. To make the decision at run time, you don't need the object built first. The only thing you need to know is the type of the actual object.
In Java, the Class (not class) object is load to the memory when it is first used. The run time knows it before any instance is constructed.
Look in your example:
Answer definitely should be 2) not 3) i.e b not c. Because if we think about run time binding and compile time binding then it should be clear. In your example very very important is that look the comments // in answer:
2)
RType.amethod // Before making any object.
99 // After making object.
RType.amethod // After making object.
So when you leave this decision at run time, it first find what type of object it is before making any instance of object. In your example,
Base b=new RType();
your Type is Base and your Base class has a Constructor and that constructor calls amethod(), which is overridden and after that
instance of object works.
Could i make you understand,
- Golam Newaz

------------------
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic