• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Exam from pgjc_Q14#-----Need help

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Who can help me to tell why the result is 22. I need show the procedur clearly. Thank you very much.
han
=================================================
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i +=v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Han,
The action starts at new Extension()

  • The Constructor Extension()implicitly calls super() i.e Base()
  • Here's the tricky one - add(1) in Extension is called (Since object type is Extension). i=2
  • The construtor Extension then calls add(2) i=6
  • When bogo is called with the new object reference, the add(8) of the object type (Extension) is called. i=6+8*2=22

  • Hope that helps. I've changed your code a bit. Do try and run this.


    rgds
    Sandeep
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys ,
very well explained...
 
hanmeng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Sandeep Potnis
thanks again
hanmeng
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there an implicit "this" being passed from sub-class ctor to super-class ctor, in this case?
Isn't it true that the sub-class's version (i.e., overriding version) of the method in question is not in the "method-table" of the super-class, so, if its (the super-class's) ctor were using its own implicit "this", then it would use the overRIDDEN version (i.e., its own version) of that method?
Since it's calling the sub-class's version, instead, mustn't it be using the sub-class's "this"?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Larry,
Sounds to me like you know way too much C++ implementation
technology for your own good! (- I'm a Lisp refugee, I had
to struggle to remember about ctor's....
Anyway, I think this is all a great deal easier if you forget
about the compiler, focus on the IS-A semantics, and remember
that method dispatch is dynamic.
"this" is a symbol useable in source code to allow you to write
a reference to the current object. The current object is what it
is, no matter who is talking about him, and there is only one
object there, not one for Base and another for Extension.
The point of dynamic method dispatch is: given an individual,
and any valid invocation of a non-private method on it such as
this.add(...), you will get the same result no matter where
the invoking code came from. It might be in Base, or Extension,
or Object; it might be in a method, a constructor, or a static
initializer; it doesn't matter: the SAME THING happens.
That is what jumped out at me about your post:
you were looking for differences in behavior of "this"
depending on where it was used, but the whole point (to me) is
that you get the same behavior no matter where it is used.
You are used to reading "this.foo(...)" and thinking aha,
"this" gets me to the method-dispatch vector of the class
whose code I'm reading. No, no! The semantics of Java is
more dynamic, halfway to lisp. "this" gets you the current
object, the INSTANCE, not any class or piece of a class.
Then method lookup starts at the class of which the current
object is an IMMEDIATE instance, and searches up the inheritance
hierarchy, and takes the first method that matches the signature
sought.
This is so, REGARDLESS of where in the inheritance hierarchy
the code appeared that invoked the method in the first place.
So FWIW, my advice is: try not to think about "this" as an
internal implementation handle on a dispatch vector. Think
about it as the object itself; that's the level of abstraction
at which it was intended to be taken, IMHO.
Of course, Java didn't go dynamic in any consistent fashion.
So fields aren't inherited like this, and constructors are
different yet again, and private methods are different,
and static methods too, etc.
These will probably seem natural to you (Larry) since they
are more compiler oriented, and more like C++. Really only
non-private method dispatch is fully dynamic in Java.
And finally -- lest you feel I'm raggin' on ya -- note that:
I missed the damn question myself. Too many different add's!
Got careless and read the add in the Base constructor as if
it was statically dispatched.
Oh well.

[This message has been edited by Jim Goodwin (edited September 18, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much for the verbose reply!
I guess one issue, however, is that, since ctors are not inherited, the implicit call to the base's ctor seems to move up into, lets call it, "the world" of the the base class. Then, a call seems to be made from within that context.
So, I guess you're saying that there is no base-class-"this" (though there is the notion of up-casting "this", which, I understand, is what "super." does); there is only the sub-class's "this", and that when an instance-method call is made, even from a super-class ctor (i.e., a method not directly known to the sub-class, whether by its own implementation or by inheritance), that sub-class-"this" is what's used in order to find the relevant method-implementation.
Exceptions being when the method is final or private, in which case, assuming a base-class reference is used, that final or private super-class implementation is used (even if there is an apparent overrider thereof, below); and, with variables, the reference-type, not the actual underlying object-type, determines which version of a "shadowed" variable is used.
And with statics, if I recall correctly, they may be "hidden" -- that is, super-class implementations of static-methods with a given signature may be, in effect, "overridden". I guess that's because their location is calculated at compile-time, as are the locations of variables in general (?). Though it seems to be bad style to "shadow" or "hide" anything.
Maybe my curiosity about "the plumbing" pays off sometimes. Other times, I realize, I might get me confused.
Thanks for your help.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
just to add my lil bit
try making class A
then class B extends A
then class C extends B
AND ALSO MAKE A METHOD COMMON TO ALL I.E OVERRIDE A METHOD SAY methCommon(){System.out.println("I am in x class");
then do
A varA = new A(); // remember the variable is of base class always
varA.methCommon();
A varB = new B();
varB.methCommon();
A varC = new C();
varC.methCommon();
watch the o/p carefully
all the files will print individual (class specific messages)
my question is How do i get access to an overriden method in a super(class B) and a super-superclass( class A here) if I have a reference variable to the base class A.
hope I could explain this a lil better :-(
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roaming Stag,
The answer is you cannot. However, if you define your method to be static, the compiler ignores object type and picks up method from type of reference.

varA.methCommon(), varB.methCommon(0 and varC.MethCommon() all execute method from class A. This is due to the fact varA, varB and varC references are of type A.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class a{
void methoda(){
System.out.println("hello a:");
}
}
class b extends a{
void methoda(){
System.out.println("hello b:");
}
}
class c extends b{
void methoda(){
System.out.println("hello c:");
}
}
public class xxx{
public static void main(String[] args){
a vara =new a();
//b vard=(b)new a() ; //line 20
a vare= new b();
a varb=new b();
a varc =new c();
vara.methoda();
varb.methoda();
varc.methoda();
vare.methoda();
//vard.methoda();
}
}
compile and run this .
It depends on the object to which it refer ,not in which you save as.If it is object to a then a ethod will call.
please explain
jaideep
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic