1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Determining which method shows polymorphism

  1. Feb 3, 2015 #1
    1. The problem statement, all variables and given/known data


    Which of them is an example of polymorphism?
    Code (Text):
    public Class D extends B
    {

    public void f()
    {
        this.g(); // 1
    }

    public void g()
    {
        super.g(); // 2
    }


    }
     
    2. Relevant equations

    Polymorphism: The ability of an object to refer to objects of other types. The ability of a method to have different functionalities, depending on the class type invoking it.

    3. The attempt at a solution

    This was what I understood to be my teacher's logic:

    Imagine a class E that extends B
    The class also rewrites method g().

    Code (Text):
    public static void main(String[]args)
    {

    E e = new E ();
    D d = new D ();

    e.f(); // calls the overriden g() method in E
    d.f(); // cals the g() method in D, which calls the g() method in B


    }
     
    I can understand why method f() shows polymorphism via this example. How is method g() not an
    instance of polymorphism though?
     
    Last edited: Feb 3, 2015
  2. jcsd
  3. Feb 4, 2015 #2

    rcgldr

    User Avatar
    Homework Helper

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted