Hai Friend,
I think so, U are test on method overloading
This might be your expectation.
pls. check & revert
package Overload;
class Animal
{
void child() {System.out.println("Animal");}
void child(Dog d) {System.out.println("Animal --> Dog");}
void child(Cat c) {System.out.println("Animal --> Cat");}
void child(Mouse m) {System.out.println("Animal --> Mouse");}
}
class Dog extends Animal {}
class Cat extends Animal {}
class Mouse extends Animal {}
class AnimalManager
{
public static void main(String args[])
{
Animal an = new Animal();
Cat c1 = new Cat();
Dog d1 = new Dog();
Mouse m1 = new Mouse();
System.out.println("Methods Overloading");
System.out.println("-------------------");
System.out.println("Methods From object of Animal Class");
an.child();
an.child(c1);
an.child(d1);
an.child(m1);
System.out.println("-------------");
}
}
----- Original Message -----
From: Asher Fawad
To: JPassion.com: Java Programming ; [email protected]
Sent: Thursday, February 16, 2012 11:33 PM
Subject: Re: [jpassion_java] Overloaded method
To tell you the truth, your code is very complex. There is no clarity
in there. If you want to learn something, learn it simply.
You should define Animal as a separate class with proper method
overloading (same method with different arguments). Put main method into a
class of its own.
I also don't see the purpose of sending arguments like "Cat c" or "Dog
d" when you aren't even using it.
What do you expect an.child(null) to print??? Of course, it might only
print "Animal" (I think it shouldn't print anything).
Your method declaration--> void child(Animal a){System.out.println("
Animal");}
should be rewritten as [ void child(){System.out.println("Animal");}
and your function call an.child(null) should be [an.child()]
Advice:
Rewrite your program with more clarity and simplicity and then tell if
you face same problem.
--- On Wed, 2/15/12, ABHISHEK TRIPATHI <[email protected]> wrote:
From: ABHISHEK TRIPATHI <[email protected]>
Subject: [jpassion_java] Overloaded method
To: "JPassion.com: Java Programming" <[email protected]>
Date: Wednesday, February 15, 2012, 11:04 PM
Hi,
Can you plz tell why output is coming as Cat not Animal?
Also if I un-comment all lines, I get compile time error.
thanks
Abhi
***********************************
package Overload;
public class Animal {
// void child(Dog d) {System.out.println(" Dog");}
void child(Cat c){System.out.println(" Cat");}
void child(Animal a){System.out.println(" Animal");}
// void child(Mouse m){System.out.println(" Mouse");}
// private void child(Cat x){}
public static void main(String[] args) {
Animal an=new Animal();
an.child(null);
}
}
************
class Dog extends Animal{}
class Cat extends Animal{}
class Mouse extends Dog{}
--
You received this message because you are subscribed to the Google
Groups "JPassion.com: Java Programming" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jpassion_java?hl=en.
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jpassion_java?hl=en.
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jpassion_java?hl=en.