• 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

Polymorphic construction

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Look at this code -
class BaseClass{
int x = 10;
public void aMethod(){
System.out.println("x = "+x);
}
}
class SubClass extends BaseClass{
int x = 20;
public void aMethod(){
System.out.println("x = "+x);
}
public static void main(String [] arg){
BaseClass bc = new SubClass();
bc.aMethod();
}
}
Options -
A.Compilation error as an instance of type SubClass
can't be assigned to instance of type BaseClass
without explicit casting.
B.Compile time error as you cannot override variable x.
C.Compiles & runs with output :
x = 10
D.Compiles & runs with output :
x = 20
I think that answer c is correct but it is d, how it is so.
Please reply urgently.
Nisheeth
Thanx in advance.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is d.
The program behaves as follows:
a variable of type BaseClass is declared and initializedto point to an instance of SubClass (which is perfectly legal) then the method aMethod is invoked upon bc which ends up executing aMethod of class SubClass (keyword: late binding !!). aMethod just prints the member variable x of SubClass whose value is 20.
If instead of bc.aMethod() you'd have bc.x then 10 would have been printed out because the direct access of member variables is based on the declared type and not the runtime type which is the case with member method invocation
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

---------------------------
(keyword: late binding !!).
---------------------------
I not sure this is a keyword. explain me.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no this is not a keyword, this was just to give a hint about which concept was explained ! Sorry I should have written (Hint: late binding)

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
Does the output remains same(i.e. 20) if the "aMethod" method will not be overridden.
And in that case does late binding is possible or not.
Great Going.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nisheeth Kaushal:
Hi Valentin,
Does the output remains same(i.e. 20) if the "aMethod" method will not be overridden.
And in that case does late binding is possible or not.
Great Going.


If aMethod() is not overridden and available in super class (as here we are using super's reference variable to store derived's object) then super's aMethod() will be called.
Late binding occurs only when there is overriden methods.
CMIW
can anyone tell me how compiler knows that he has to do late binding for this method?? in c++ we define function to be 'virtual' and so compiler knows that there late bindinng is to be done.
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited October 23, 2001).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravish kumar:
[Bcan anyone tell me how compiler knows that he has to do late binding for this method?? [/B]


One of the big differences conceptually between C++ and Java is that in Java, all methods are, by default, "virtual." (Virtual is the keyword used in C++ to indicate that late binding is required.) If you don't want it to be "virtual," you can declare it final. Most of the Java for C++ programmer tutorials I've seen make a big deal out of this, since many C++ programmers are used to the non-virtual default behavior in C++.
Bill
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravish.
The compiler invokes statically, that is, depending on the declared type of the reference the following:
static, private, and constructors invocations.
The methods that are not mentioned in the previous parragraph are invoked dinamically: they are invoked based on the type of the object pointed to by the reference.
There is an especial case: super.method() invocations. With compilers up to Java 1.0.2 the binding was static. With Java 1.0.2 and beyond an especial kind of dinamic binding is performed: at runtime the nearest superclass that defines the method is chosen.
To check this out, use javap -c YourClass to see the JVM instruction that performs the call.
Note that in Java all the methods are bound at runtime, because the compiler produces symbolic references for the invocations of methods and they are resolved only at runtime. In Java, the JVM serves as a linker/optimizer.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
The compiler invokes statically, that is, depending on the declared type of the reference the following:
static, private, and constructors invocations.
The methods that are not mentioned in the previous parragraph are invoked dinamically: they are invoked based on the type of the object pointed to by the reference.
There is an especial case: super.method() invocations. With compilers up to Java 1.0.2 the binding was static. With Java 1.0.2 and beyond an especial kind of dinamic binding is performed: at runtime the nearest superclass that defines the method is chosen.
To check this out, use javap -c YourClass to see the JVM instruction that performs the call.
Note that in Java all the methods are bound at runtime, because the compiler produces symbolic references for the invocations of methods and they are resolved only at runtime. In Java, the JVM serves as a linker/optimizer.


Jose !!!
When are you going to take your exam? I am already looking forward to the tips. Perhaps, don't wait for the exam, could you please let us know which books/notes/whatever you are referring to. Your knowledge and in depth understanding of java is awesome.
- Manish

[This message has been edited by Manish Hatwalne (edited October 23, 2001).]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I took the exam now I would fail for certain because I haven't studied all the objectives yet.
Because I am not (still;-) a programmer I decided to deep my knowledge of the internals of Java to be able to understand the language itself. For this I have read Inside The Java 2 Virtual Machine by Bill Venners. Some on line chapters can be found at artima.com
reply
    Bookmark Topic Watch Topic
  • New Topic