Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Polymorphism Question

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Polymorphism Question

    Hello! This is my first time in this forum. I hope that you guys can help me on this theory question..
    It is about polymorphism and I'm not too good at it.
    class Food{
    	public String show(Food f){
    		return("Food Food");
    	}
    /*	public String show(Fish f){
    		return("Food Fish");
    	}*/
    }
    class Fish extends Food{
    	public String show(Food f){
    		return("Fish Food");
    	}
    	public String show(Fish f){
    		return("Fish fish");
    	}
    }
    class Poly{
    	public static void main(String args[]){
    		Food fo = new Fish();
    		Fish fi = new Fish();
    		System.out.print(fo.show(fi));
    	}
    }
    --------------------------------
    class Food{
    	public String show(Food f){
    		return("Food Food");
    	}
    	public String show(Fish f){
    		return("Food Fish");
    	}
    }
    class Fish extends Food{
    	public String show(Food f){
    		return("Fish Food");
    	}
    	public String show(Fish f){
    		return("Fish fish");
    	}
    }
    class Poly{
    	public static void main(String args[]){
    		Food fo = new Fish();
    		Fish fi = new Fish();
    		System.out.print(fo.show(fi));
    	}
    }


    Why is it that code 1 gives me Fish Food
    and Code 2 gives me Fish Fish

    The only difference with them, is the commented line. THANK YOU!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Polymorphism Question

    I'm not really sure what your question is- that's just how it works, according to the JLS. What did you expect to happen?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism Question

    Hmm, do you by any chance have a link to a good tutorial which involves simple polymorphism and objects passing into them?
    I'm just very confused with polymorphism when it comes to,
    B1 b = new B();
    A1 a1 = new b();

    If its just B1 b = new B passing through, i understand how it works. but when A1 is b .. that is where i get lost =(.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism Question

    I did google for that and went through a few youtube videos already. But none of them have that type of example that i'm looking for sadly..

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Polymorphism Question

    What type of example are you looking for, exactly?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism Question

    The type where an object is brought into a class and there should be a method 'calling' the object. Kinda hard to describe it.
    But if you look at the code at the first post and espacially to
    Food fo = new Fish();
    Food = fish...
    Almost all examples that i have went through, they do not pass objects into methods..

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Polymorphism Question

    Quote Originally Posted by kutysam View Post
    Almost all examples that i have went through, they do not pass objects into methods..
    That must mean you're looking at some basic tutorials, which is fine- but I wouldn't bother focusing on the finer points of OOP if you're still on the basic tutorials. It'll come to you in time. Also, don't just read through the tutorials- write tons of code, and when you're sick of writing code, write some more code. That way you don't get ahead of yourself before fully understanding the current lesson.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism Question

    I have eventually understood it!

    In Code 1, there is no show(Fish f) means it will IGNORE ALL subclassess with method carrying (Fish f) in it.
    The first line that it reads will thus be in Fish Class, (Food f).

    In Code 2, since there is a Fish f in the main class, it will go to
    1)Fish class and find if there is a fish f.
    If there isn't it will go to 2) Food Class and find if there is a Fish F
    If there isn't it will go to 3) Fish Class and find if there is a Food F Since this is the inheritence thing
    If there isn't it will go to 4) Food Class and read Food F << There HAS to be this otherwise, compilation error

    Correct me if i am wrong please! Thanks!

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Polymorphism Question

    It seems like you could correct yourself even more easily (and definitely more reliably) if you simply created a basic program that tested your theories. Then you can report your findings to us!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism Question

    Anyway, hope you dun mind explaining this very simple type casting to me.
    New x = new Newer();
    *Assume the two classes are above*
    and Newer extends New

    so if i put (Newer) x

    Does this mean temporarily, x changes to newer ?
    Newer x = new Newer ?

    Or there is no changes to x ?

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Polymorphism Question

    Again- what happened when you tried it? Did you write a simple program to test what happens in different scenarios? With more than one subclass?

    We aren't a compiler- the compiler is a compiler.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM
  2. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 03:26 PM