Originally posted by nachiket deshpande:
pl.look at the following code
class mammal{
void eat(mammal m){
System.out.println("mammal");
}
}
class cattle{
void eat(cattle c){
System.out.println("cattle")
}
}
class horse{
void eat(horse h){
System.out.println("horse")
}
}
public class animal{
public static void main(String[] args){
mammal m=new horse();
cattle c=new horse();
horse h=new horse();
c.eat(m); //last line
}
}
look at the last line..
the o/p is "mammal"
if i call h.eat(c);the o/p is "cattle"
if i call c.eat(h); the o/p is "cattle"
can some one help me how the overloaded methods are called here???
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Muhammad Ali Farooq
Originally posted by nachiket deshpande:
Hi Farooqmali
Can you please expalin me how the methods are called and also the output...
Originally posted by Rick Zhong:
Hi nachiket,
the result coming out like these is because it has nothing to do with overriding or dynamic binding, the method eat() invoked according and ONLY according to the [b]reference type!
however, i have a ques about overloading term itself, can we say the three eat() are OVERLOADING methods even though they are not defined within the same class!?
anyone can clarity it?
Rick[/B]
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.
Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Originally posted by Bob Vel:
Hello Lam Thai
According to your answer
So if you print m.eat(cattle c), you infact tell java to "invoke method eat() that belongs
to class mammal." That is what the instant 'n' stands for. That is why you get "mammal".
When I run this following code I am getting output as
cattle
cattle
cattle
Why?
As per your answer, I thought it should print
cattle1
cattle
cattle
Please explain.
class mammal
{
void eat(mammal m)
{
System.out.println("mammal");
}
void eat(cattle c)
{
System.out.println("cattle1");
}
}
class cattle extends mammal
{
void eat(cattle c)
{
System.out.println("cattle");
}
}
class horse extends cattle
{
void eat(horse h)
{
System.out.println("horse");
}
}
public class animal
{
public static void main(String[] args)
{
mammal m=new horse();
cattle c=new horse();
horse h=new horse();
m.eat(c);
c.eat(h);
h.eat(c);
}
}
Thanks in advance.
Originally posted by ratul banji:
Hi lam,
I want some more explanation 4rm u.All that u explain is quite good.
But...pls,explain me this portion1ce again.That will be very kind of u.
<quote>
Now when you invoke c.eat(cattle c) or c.eat(horse h), you would get an answer of "cattle" for the same reason stated above (i.e you invoke eat() method in cattle class, That what 'c.' stands for).
</quote>
In the case of horse:
The class horse can response to three different eat()'s. Therefore, in this case the signature or the argument will dictate what method is to be called. why it is not heppening for 'cattle' also ....as cattle also can response to 2 different eat()'s ?
Thanks in adv.
Regds.
[b]<marquee> Ratul Banerjee </marquee>
[/B]
Shah Chunky - Sun Certified Java2 Programmer.
Originally posted by Shah Chunky:
Hi Lam...
Can v say that Overloaded Methods r invoked using the reference type & not the type of object it referrs to as in case of Overriding.
Also i have made some changes in the code & it is giving me a strange error can u help me...
class mammal
{
void eat(mammal m)
{
System.out.println("mammal");
}
}
class cattle extends mammal
{
void eat(cattle c)
{
System.out.println("cattle");
}
}
class horse extends cattle
{
void eat(mammal m)
{
System.out.println("Mam Cat");
}
/*void eat(cattle c)// Commenting this method gives CTE
{
System.out.println("Cat");
}*/
void eat(horse h)
{
System.out.println("horse");
}
}
public class Animal2
{
public static void main(String[] args)
{
mammal m=new cattle();
cattle c=new cattle();
horse h=new horse();
m.eat(h);
c.eat(m);
h.eat(c);
System.out.println();
}
}
It gives me the below Error at Compile time
Animal2.java:41: Reference to eat is ambiguous. It is defined in void eat(mammal) and void eat(cattle).
If i remove the Comment it works fine.
Thanks in adv
Originally posted by vivek bawge:
Lam,
Thanks for your nice explanation. But i have another doubt here.
Does overloading methods belonging to the same class and overloading methods in the class with the methods derived from the base class work differently? Why?
I thought that all the base class methods come in the name space and scope of derived class. (Ignoring overriding also)
I mean, if compiler is confused now when the two methods are in the same class, then, why it could resolve the call in the original example, when 1 method was in horse and another in cattle.
Thanks in advance,
Vivek
Originally posted by vivek bawge:
Hi Lam,
thanks for the elaborated reply. And i do follow the concepts after each code snippet. I am sorry but when i put together 2 code snippets it doesnt make much sense to me. And as per your concluding remark, it doesnt have anything with the combined effect of overlading and inheritence. But i realize that things are little different though. Lets forget about the world and some other supporting examples and just look at the 2 examples below.
I would appreciate if you just tell me the brief answer as to why compiler gets confused in the first case and resolves the second case.
THE ONLY DIFFERENCE IS in the case1 eat(Cattle) is in the base class Cattle and in case 2 eat(Cattle) is in the derived class Horse. But Either way isnt it part of Horse ( because in case 1 also its derived from the base class Cattle).
//case 1
class mammal {
}
class cattle extends mammal {
void eat(cattle c) {System.out.println ("Cattle in cattle");}
}
class horse extends cattle {
void eat(mammal m) {
System.out.println ("Mammal in horse");
}
}
class Demo {
public static void main(String[] args) {
horse h = new horse();
h.eat(new mammal());
h.eat(new cattle());
h.eat(h);
}
}
//case 2
class mammal {
}
class cattle extends mammal {
}
class horse extends cattle {
void eat(mammal m) {
System.out.println ("Mammal in horse");
}
void eat(cattle c) {
System.out.println ("Cattle in cattle");
}
}
class Demo {
public static void main(String[] args) {
horse h = new horse();
h.eat(new mammal());
h.eat(new cattle());
h.eat(h);
}
}
Thanks a lot for the patience.
Hope you will help me.
sincerely,
Vivek
Don't get me started about those stupid light bulbs. |