Hi Friends, This code is clearer but there is still some confusion (I think). My feeling is that this is a complex example of method overloading. It feels like Class Dog, Cat and Mouse should have been defined before Animal (I have forgotten if it really matters so I will move safely). Then Animal class will have a child method whose arguments will be different user created data structures (namely classes Dog, Cat and Mouse). Then in main, you can create one instance of Animal class and call the child method on it with different arguments such as an.child();an.child(d);an.child(c); and so forth alternatively, you can let AnimalManager extend Animal class and then you can use child methods without the need of a reference. Please correct me if I am wrong somewhere. ThanksAsher
--- On Thu, 2/16/12, Saravanan <[email protected]> wrote: From: Saravanan <[email protected]> Subject: Re: [jpassion_java] Overloaded method To: [email protected], "JPassion.com: Java Programming" <[email protected]>, [email protected] Date: Thursday, February 16, 2012, 8:50 PM 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.
