Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Help! exam is tomorrow... OO question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main (String[] args) {
A c1 = new C();
C c2 = new C();
c1.m1(c2);
}
}
What is the result of attempting to compile and run the program?
The answer is Prints: A
The answer to this question conflicts with a similar question on a Sun mock exam that I've been studying - in that exam - they say that the m1() method would be invoked in class C not A - in fact they say there is no way to invoke the super class's m1() method using an instance of class C created in some other class like D.
I'm confused and I really need help/explanation fast. My exam is tomorrow morning.
Thanks!
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 3 method all named m1 but have different argument type; one is A, another is B and the other is C. So these methods are not overriding their parent method. In this case, object instance is looking for the method whose type is same as itself, which is A. So it calls A's m1 method. A's m1 method takes type A as an argument and C is a sub sub class of A and upcasting is occured without any problem.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan,
Look at how object c1 is instantiated:
A c1 = new C();
C1 is of type A. So at compile time it is bound to the method m1()defined in class A as it does not know that there are similar m1()methods in other class B,C or D.Hence it prints A.
However C2 is of type C.So if you call c2.m1()then at compile time it looks at class C and binds it to method m1() defined there and hence you will get C printed.
Try changing the reference c1 to B and D and you will see how the binding works.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To illustrate what I said previously.
If you change class C from
class C extends B {
void m1(C c) {System.out.print("C");}
}
to
class C extends B {
void m1(A c) {System.out.print("C");}
}
You'll get what you expected.
Hope this helps.
 
Dan Lajoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, guys I think I'm seeing the light. Here is the question in the Sun mock exam which is confusing to me:
The GenericFruit class declares the following method to return a float number of calories in the average serving size.
public float aveCalories()
Your Apple class, which extends GenericFruit, overrides this method. In a DietSelection class which extends Object you want to use the GenericFruit method on an Apple objct.
Select the correct way to finish the statement in the following code fragment so that the GenericFruit version of aveCalories is called using the gf reference or select option d.
1. GenericFruit gf = new Apple();
2. float cal = //finish this statement using gf
a. gf.aveCalories();
b. ((GenericFruit)gf).aveCalories();
c. gf.super.aveCalories();
d. There is no way to call the GenericFruit method.
I thought the answer should be a, but it's d. Doesn't line 1 above bind the gf obj to the GenericFruit class - thus also binding it to the superclass aveCalories() method?
Thanks!
 
La Vish
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important points in this question:
1.GenericFruit class has public float aveCalories()
2.Apple class EXTENDS GenericFruit Class
3.Apple class also has public float aveCalories() meaning it has
overridden the one in the GenericFruit Class.
Now look at how gf is instantiated:
GenericFruit gf = new Apple();
This means gf is of type GenericFruit but is actually an Apple object.
Now you thought gf.aveCalories() would have called the one in the
GenericFruit class. Let us see how the compiler looks at this code.
It goes to GenericFruit class and looks at its public float aveCalories()
method and says "hang on, this object belongs to my child called Apple and it has
the same method and therefore I am going to use it " and binds
itself to the method in the Apple class.REMEBER GenericFruit knows that
it has a child class Apple because Apple extends GenericFruit.
Code and see what happens if you change as follows:
1.GenericFruit gf = new GenericFruit();
gf.aveCalories();
2.Apple gf = new Apple();
gf.aveCalroies();
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan
If you wanna go technically then the method that is being invoked is C's method and not A's method as in your first post. But if you see that what is a C's method. It is simply an inherited method from A. The method has been inherited from A and has not been overridden that means the same functions that the method did in the class A should be performed in class C. So actually the method invoked at runtime is actually a method from class C inherited from A.
[ May 28, 2003: Message edited by: Anupam Sinha ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the below program.
class A {
void m1(A a) {System.out.print("AA");}
void m1(C c) {System.out.print("CC");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D20 {
public static void main (String[] args) {
A c1 = new C();
C c2 = new C();
c1.m1(c2);
}
}
This prints C. When a method is overridden and when a sub-class object is assigned to super class reference, then only the sub-class method is called or else super class version only be called. If i am wrong pls. correct me. Thx.
 
Dan Lajoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. You guys have been very helpful. Thanks very much! this was my first time out here to this or any other forum from this site. Very impressive.
I'll be back! Now I'm off to take the exam.
Thanks again.
Dan
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck on your exam.
I've been waiting for myself to become confident to take the exam for two years now.
I should take one someday soon.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, post your experience/results in the other forum. Thanks!
 
Dan Lajoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok will do. I passed!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic