• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inherited Method Calls from Mock

 
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,

This question is from one of Mr. Dan Chisholm's mock exams, which can be found at http://www.danchisholm.net/dec20/guide/chapter6/exam1.html

The question number on the mock exam is 20.

Here is the code for the question:

MY QUESTION: Why does the code output A? I thought that if a non-static method was overridden, then the most specific method ought to be called because the actual object is of type C. Isn't this the basis of polymorphism?
 
Pal Sudarshan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer may be that in that question, the methods are not being overriden but are being overloaded. What are your thoughts?
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone take time to answer the question given above by PAL.
I am also waiting for a detailed answer.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings from Hogwarts,

Bro, u are right.. I think the methods are not being overridden.. they are being overloaded. The subclasses have the inherited m1 method from parent but then they define their own overloading m1.

We should identify that overriding implies an EXACT MATCH of the arguments (among other rules) and nothing less.

So class C will have three m1() s, one that it overloaded and the other two inherited from its ancestors.

But when u say A c1=new C().m1(new C()), which method is called, is decided at compile time based on overloading... Since c1 is declared a reference variable of objects of A, m1() in class A is called.
That's why it prints "A".

BUT SUPPOSE, if class A has other methods and class C also inherits them (through class B),

then, when you call a object method on the object passed, THEN POLYMORPHISM PLAYS ITS ROLE, and the overriding implementation in the subclass C will be called..

Hope I haven't confused the whole thing..

All I want to say is,

suppose the

class A {

void m1(A a) {
System.out.println("A");
a.m2();
}

void m2() {

System.out.println("A");

}
}

and m2() is added as above in the classes B and C also ( but the prints will be B and C respectively),

then for the code, the output will be

A -- //this is at compile time --NO POLYMORPHISM
C -- //this is decided at runtime -- POLYMORPHISM
[ July 16, 2004: Message edited by: Brian Percival ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that m1 in B and C have different signatures causes the runtime engine to call the method in A. This causes earlier code using the method call to A.m1 to still call the right method m1 in A even if they are passed a newer object type inheriting from A.

A method signature is composed by the list of arguments and the return type. Note that the compiler will flag the fact that you can't override a method by just changing the return type, but will stay silent to the fact that you use a subclass of one type in the signature as you are entitle to use it that way.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE:
_______________________________________________________________________
MY QUESTION: Why does the code output A? I thought that if a non-static method was overridden, then the most specific method ought to be called because the actual object is of type C. Isn't this the basis of polymorphism?
_______________________________________________________________________

The code above outputs A because when you create
A c1 = new C();

what happens is that the object "c1" of class A gets created. It gets instantiated by default constructor for class C i.e. C().
The default constructor of subclass C calls default constructor of superclass B().Now subclass B() calls superclass A's default constructorA()in turn.

You can check this by using the following code.

_______________________________________________________

class A
{
A(){int k=1; System.out.println("k="+k);}
void m1(A a)
{ System.out.print("A"); }
}


class B extends A
{
B(){int j=1; System.out.println("j="+j);}
void m1(B b)
{ System.out.print("B");}
}

class C extends B
{
C(){int i=1; System.out.println("i="+i);}
void m1(C c)
{ System.out.print("C");}
}

class D
{

public static void main(String[] args)
{

A c1 = new C();
C c2 = new C();
c2.m1(c2);

}
}
____________________________________________________
This gives an output like
k=1 j=1 i=1//c1 instantiated
k=1 j=1 i=1//c2 instantiated
C //prints C
prints C as you wanted. Incase you use the call by c1.m1(c2) "A" will be printed and so for other objects too.This will happen irrespective of the class you call to instantiate the object i mean..
A c1 = new B();
B c3 = new C();
C c2 = new C(); //can't be A() as this A is the superclass.
c3.m1(c2);//c3 is object of B so B will be printed.This wasn't
//the case earlier.

Overloading states that

Overload: you use different arguments and can change the method but with the same name. Constructors can be overloaded not overridden.

Overridden: They have to be inherited. Allows you to change the method implementation but should have not only the same name but also same type of arguments and return type.

The methods are overloaded not overridden as Brian said.
Hope it helps...... :roll:
[ July 17, 2004: Message edited by: natarajan raman ]
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic