• 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

Polymorphism

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Khalid Mughal.





Select the correct answer :
a) The program will fail to compile.
b) The program will compile with out error and print 0
c) The program will compile with out error and print 1
d) The program will compile with out error and print 2
e) The program will compile with out error and print 3

The Correct Answer is
c) The program will compile with out error and print 1



I was expecting the answer to be d ie. the result should print out 2 .

Because the object is of C type even though it has a reference of B.
It should call the methods of C type unless the method is declared static.
(and thne it uses the reference type)
So where am i going wrong...?
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a C is a B. When you cast it to B, you call the B implementation.

Without the cast, you would get a call to the C implementation.
[ August 27, 2008: Message edited by: James Clark ]
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because f() method is private in class A & B. So it f() is not overriden in class B and C.Those are different methods. So at the time of invoking method f() by method g() it is selecting the reference type not the object type.So method f() of class B is selected. If you change f() to private in class A & B ,output will be 2.
[ August 27, 2008: Message edited by: subhasish nag ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE 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 James Clark:
a C is a B. When you cast it to B, you call the B implementation.

Without the cast, you would get a call to the C implementation.



No you are wrong....



In this code all the calls to g() will return 1...This is because in all the cases the g() in class B will be called. f() is private in B so it is not inherited...So the f() method in C is not overriding f() method of B. this is why call to f() in g() method of class B will always call f() method in class B.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here Class C is not overiding the method g(). So the B's implementation of g() method will be called though C inherited the g() method (not overiden).
 
Ranch Hand
Posts: 30
Hibernate jQuery Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt...
If you are saying that (B)ref1 is C yet,and then call f()... in runtime the JVM would call C's f(),but why when i do this...

the compiler complains about private method in B class...

Sorry for poor english!
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler is giving you error because the method f() in class B is private.

At compile time methods are checked in correspondence to the type of the reference with which they are called.

Eg


The above code will give you an error that display is not a member of A. This is because the reference aObj is of type A and it denotes an object of type B. But during compilation compiler will only consider the type of aObj(which is A) and check if the method is available in A class or not. Since the method is not available in class A so it will not allow you to call it. However if you change the code-

public static void main(String[] args)
{
B bObj = new B();
bObj.display();
}

Now the compiler will allow you to call display as you are calling the method with bObj which is of type B and class B has a method named display.

Your case is also similiar. The method f in class A is private. So the compiler will check if f() method of class A is accessible or not. Since it is private so it cannot be called. Now you might say that the f() method in class C will be called which is public. But this will happen on runtime. During compilation the compiler doesn't know that ref2 actually denotes an object of type C. What it knows is that ref2 is of type A and f() method in A is private.......

fffeeeewwwwww....
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very Good Clear Explanations @ ANKIT
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question...

The compiler checks that f() method is private in B and is not inherited by C.And g() is not overridden by C. So it decided during compile time it will call method of B.

So it calls the method g() of B which is a reference type of the Object and then calls the corresponding method in f()

Am I Right?


Ankit, Thaks for your lengthy explanation...
and of course thanks to the rest of you
 
Gabriel Ozeas
Ranch Hand
Posts: 30
Hibernate jQuery Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit, i understood now... this toppic is a little confusing, but i understood!
[ August 28, 2008: Message edited by: Gabriel Ozeas ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic