Results 1 to 2 of 2
  1. #1
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default question on overriding or polymorphism?

    I don't know if my question is related to overriding or polymorphism hence looking forward for someone advise.

    Refer to the following coding,


    Java Code:
     
            Father f=new Father();
            f.eat();             // display father's eat()
            Son s=new Son();
            s.eat();             // display Son's eat()
            Father x=new Son();
            x.eat();	         // display Son's eat()



    It is learned that


    Son s=new Son();
    s.eat();


    and

    Father x=new Son();
    x.eat();


    would display the same result, then I wonder why some tutorial are mentioned about using

    Father x=new Son();
    x.eat();

    instead of using

    Son s=new Son();
    s.eat();

    directly?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: question on overriding or polymorphism?

    It can be useful to reference a bunch of different subclasses by either their superclass type or by a common interface they have implemented. It is usually the case when you want to invoke the same method on a list of those subclasses. The list is stored as either a superclass type or the common interface type.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. question about return type of overriding the method
    By thinkinjava24 in forum New To Java
    Replies: 6
    Last Post: 01-15-2013, 07:33 AM
  2. A question about inheritance / polymorphism
    By gabriels in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 11:53 AM
  3. Overriding question
    By alacn in forum New To Java
    Replies: 1
    Last Post: 08-16-2010, 11:04 AM
  4. Replies: 45
    Last Post: 05-25-2010, 08:32 PM
  5. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •