aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in overridiing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in overridiing" Watch "Doubt in overridiing" New topic
Author

Doubt in overridiing

Venkatachalam Palanisamy
Greenhorn

Joined: Aug 04, 2000
Posts: 12
Hi,
class Base
{
void method(Base b)
{
System.out.println("Its in the base");
}
}
class Derived extends Base
{
void method(Derived f)
{
System.out.println("Its in the derievd");
}
}
class Deriv extends Derived
{
void method(Base k)
{
System.out.println("its in the Deriv");
}


public static void main(String [] a)
{
Deriv hj=new Deriv();
hj.method(hj);
}
}

Can any one tell me why its giving compile time error like this.
Reference to method is ambiguous. it is defined in the void method(Base) and void method(Derived).
hj.method(hj);
^

venkat.
rajni_singh
Greenhorn

Joined: Oct 30, 2000
Posts: 7
Hi Venkat !
See this is the sequence in which things happen :
1)When Derived extends Base, Derived contains
void method(Base) as well as void method(Derived). Here method is overlaoded and not overridden as method has diff parameter types.
2)When Deriv extends Derived, Deriv contains
void method(Base) as well as void method(Derived). Deriv overrides the method(Base).
3)When an instance of Deriv is created and an object of Deriv is passed as a parameter, there is confusion.
void method(Base) can accept Deriv as a parameter and
void method(Derived) can also accept Deriv as a parameter as
Deriv is a subclass of both Base and Derived. Hence the compiler
gives the unambiguity error.
Hope I am clear !
Sujit Kurtadikar
Ranch Hand

Joined: Dec 05, 2000
Posts: 68
Hi Venkat,
method(Base b) is defined in class base. which is then inherited in class Derived.
In class Derived you are defining method(Derived f), this is not the overriding, but you are
defining separate method with different signature.
In class Deriv 2 methods are inherited,
1. method(Base b) from class Base
2. methos(Derived f) from class Derived.
In class Derived you are overriding method(Base b) method, this is OK.
But problem is in calling function like,
Deriv hj = new Deriv();
hj.method(hj);
Now for this function call, two methods are applicable
1. method(Base b) & 2. method(Derived f)
Since Base is base class, it can take object of class Deriv.
same is case for Derived, method(Derived f) can also take obj hj.
This means that there is is ambiguity for compiler to decide which methos to call. Thats why such
compile error is comming.
If you defined method(Deriv de) in class Deriv, then this error will not come. OR
Regards,
Sujit
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I saw a case very similar to this not long ago where it was decided at runtime to use the subclass method. the only difference I notice between the two is the overriding of method(base b). In the situation im thinking of the parameter passed was an object and there were two methods one took an Object and the other took a String. the one that took the string was chosen at runtime. no error. They might have been constructors(I dont remember).


SCJP
Visit my download page
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi All,
JLS �15.12 lays out the rules for method invocation. There are two basic steps:

  1. Find the methods that are applicable and accessible
  2. Choose the most specific method

  3. In this example:

    1. two methods are applicable and accessible
      <pre>
      method(Base k)
      method(Derived f)

      </pre>
    2. the rule for determining the most specific method is:
      <quote>
      The informal intuition is that one method declaration is more specific than another if any
      invocation handled by the first method could be passed on to the other one without a
      compile-time type error.
      </quote>
      In the example, hj 'is a' Base object as well as a Derived object. It can be cast to either type without causing a compile-error. This makes the method call ambiguous ie either method could be used and rather than having the compiler make the final decision, an error is thrown, pushing the responisiblity for the final decision back to the programmer.

    3. Randall, I think the example you're referring to involved a System.out.println(null) call. In that situation, 'null' is simply converted to a String, just as System.out.println(5) would be.
      Hope that helps.
      ------------------
      Jane
      The cure for boredom is curiosity.
      There is no cure for curiosity.
      -- Dorothy Parker
      [This message has been edited by Jane Griscti (edited December 05, 2000).]
      [This message has been edited by Jane Griscti (edited December 05, 2000).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Hi friends
consider this
class one{
void method(int e){System.out.println("one");}
void method(long l){System.out.println("oneone");}
public static void main(String []a){
int i=5;
one r=new one();
r.method(i);
}
}
what do think which method will be chosen during run time?
sure it shoud be the one which is holding int type parameter
even though int can be casted to long.It means if we are sending
a value to a method and there are overloaded methods avialable
the the compiler will choose the one whose parameter is of exactly same type(type of the value).
Now see this

class one{ void method(int e){System.out.println("one");}

}
class two{
void method(long l){System.out.println("two");}
public static void main(String []a){
int i=5;
two r=new two();
r.method(i);
}
}
the output is 'two' .
But why??
when we extend a class all methods are inherited to sub class.
So compiler should choose the 'int' one like it did in the last
example.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

because you forgot to extend one when you declared class two?
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
oh! my mistake...
Here I go again..
class one{ void method(int e){System.out.println("one");}
}
class two extends one{
void method(long l){System.out.println("two");}
public static void main(String []a){
int i=5;
two r=new two();
r.method(i);
}
}
Now still the same problem as I mentioned in my last post.
Could anyone explain that....
Thanks Randall
[This message has been edited by Nasir Khan (edited December 05, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in overridiing