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 2 of 2

Thread: Polymorphism Question

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Polymorphism Question

    Hi today I got this question on my java exam regarding polymorphism.

    Hamburger h = new Hamburger();
    FastFood food1 = h;
    Food food2 = new Hamburger();
    Item Item1 = food2;

    "What would be the difference between using the object food2 and the object h? " - Write down all your assumptions.

    I would start with the assumption that that Food is a superclass of Hamburger.
    But then to the unanswered question. What would the difference between the object food2 and the object h?
    As i understand polymorphism both objects still get the method defined in Hamburger. So what would be the difference?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polymorphism Question

    what would be the difference?
    Try writing a small program with those classes and methods to demonstrate what happens. Have the methods print something to show which method was being executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help me learn - Polymorphism
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2014, 03:36 PM
  2. Polymorphism
    By gautammuktsar@gmail.com in forum Object Oriented Programming
    Replies: 1
    Last Post: March 5th, 2014, 03:15 PM
  3. Polymorphism Question
    By BuhRock in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 11:09 AM
  4. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  5. Polymorphism Question
    By kutysam in forum Java Theory & Questions
    Replies: 11
    Last Post: August 30th, 2011, 11:59 AM