• 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

Can any1 explain this??

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the result of attempting to compile and run this ?

class Base{
String s = "Base";
String show(){
return s;
}
}
class Derived extends Base{
String s = "Derived";
}
public class Test {
void print(Base b){
System.out.println(b.show());
}
void print(Derived d){
System.out.println(d.show());
}
public static void main(String[] args){
Test t = new Test();
Base b = new Derived();
t.print(b);
}
}
correct answer/s : will print "base"
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i understand:

void print(Base b) is a 'more specific method' than void print(Derived d) because Derived is-a Base and not vice versa.

So irrespective of whether the object is of type Base or Derived, the call is always going to go to the method void print(Base b).
i.e.,
If object obj is declared: Base obj = new Base() or Derived obj = new Derived() or Base obj = new Derived()
t.print(obj) always goes to void print(Base b) and hence prints 'base'

For a complete discussion of 'most specific method' see Corey's blog from 4 days ago.
 
Sandya Bhaskara
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rajan...
i think ur explaination is correct.thanx..but i have another question in the above code,replace t.print(b) with b.show()..ie at runtime it will be new Derived().show()...but the output is "Base"..i thought it would print "Derived"...the String s in derived is hiding the String s in Base..but it is the String in base that gets printed..how come??
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys ,

this is Sandhya's program ...

no matter what u do it prints . Base i'm in base ..

why does this program act like this ??? try this program uncommenting the code i have commenting & with comment signs ..makes no difference ..
am i too sleepy ???
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandya Bhaskara:
hi rajan...
i think ur explaination is correct.thanx..but i have another question in the above code,replace t.print(b) with b.show()..ie at runtime it will be new Derived().show()...but the output is "Base"..i thought it would print "Derived"...the String s in derived is hiding the String s in Base..but it is the String in base that gets printed..how come??



you're calling "show()" from the Base class, which uses its reference to the String "s", and therefore prints out "Base." if you override the method in the Derived class, it will print the String "s" in Derived, (i.e., to get "Derived" to return from b.show(), copy and paste the "show()" method into the Derived class) but the way you have it set up using implicit default constructors and the same name for data members it's pretty hard to trace out what is happening.

[ August 23, 2004: Message edited by: Chris Wash ]
[ August 23, 2004: Message edited by: Chris Wash ]
 
Sandya Bhaskara
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U can't uncomment the print(base)method..u'll get compile time error if u do that...it says something like this..print(derived) cant be applied to (base)...
 
Sandya Bhaskara
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for the explanation chris....the ques is from sahirs mock test.. very tricky..do we get such questions in the SCJP exam??
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sandhay ..even i was expecting something like that ... but nomatter what changes i make in the SOP of print(base) method .. its still printing ..Base i'm in base. i dont know something is wrong here .. i'm confused already ..its makin me go crazy ...
 
Chris Wash
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I'm guessing the question is geared towards testing how much you know about implicit default constructors and inheritance. Take a closer look at how those constructors get generated and it should make a little bit more sense.
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chris , just look at the changes i made in program . according to that ..the program shud not even compile .. there is some prb with my system i guess .. anyways i thankx
 
Chris Wash
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the confusion... here is an answer that works. i had totally forgotten about this until re-reading some of my old school notes.

this is actually an example of shadowing. the string s is declared in both the superclass, base, and the subclass, derived; whenever you're trying to access a shadowed variable using an object reference, the type of the reference determines which of the variables is used. since b is declared as of type base, the reference type is a base, although the actual class of the object is derived. variable shadowing does not work exactly like overriding methods -- whereas overriden methods are looked up dynamically using the class of the object, shadowed instance variables are looked up by the type of the reference!

i'll post a little blurb from the brogden/green book i have:


Can You Override a Variable?

Yes, you can override a variable. This is the short answer. A subclass can have a variable with the same name as a variable in the parent class. In this case, if the parent class variable is not private, the subclass variable is said to shadow the parent class variable. However, there is a significant difference between the way the Java compiler treats methods and the way it treats variables. Because of the way variables are stored in the memory allocated to an object, references to variables can be computed at compile time based on the type of the variable.

Suppose that the BookElement class defines a variable named lineCt and the subclass Chapter defines another variable named lineCt. Which value would the following code fragment print?

Chapter ch = new Chapter() ;
BookElement b = ch ; // casting to the parent class
System.out.println( b.lineCt ) ;


If you guessed that the compiler would use the lineCt variable found in the BookElement class because b is a BookElement reference, you are correct.

Remember that references to member variables are computed at compile time using the type of the reference. References to member methods are resolved at runtime using the type of the object.


[ August 23, 2004: Message edited by: Chris Wash ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic