• Support PF! Buy your school textbooks, materials and every day products Here!

Determining which method shows polymorphism

  • #1

Homework Statement




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

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

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


}

Homework 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.

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:
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:

Answers and Replies

Related Threads on Determining which method shows polymorphism

Replies
2
Views
1K
  • Last Post
Replies
1
Views
871
Replies
9
Views
11K
Replies
6
Views
3K
Replies
5
Views
689
Replies
1
Views
4K
Replies
1
Views
3K
Top