Determining which method shows polymorphism

  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Method
AI Thread Summary
The discussion centers on identifying polymorphism in Java through method overriding. The example provided illustrates that method f() in class D demonstrates polymorphism by calling an overridden method in class E. In contrast, method g() does not exhibit polymorphism because it directly calls the superclass's implementation without any overriding. The distinction lies in the ability of a method to invoke different functionalities based on the object type. Ultimately, understanding these concepts is crucial for grasping polymorphism in object-oriented programming.
friendbobbiny
Messages
49
Reaction score
2

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:
Physics news on Phys.org

Similar threads

Replies
5
Views
3K
Replies
2
Views
2K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
5
Views
3K
Replies
17
Views
2K
Back
Top