Determining which method shows polymorphism

  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Method
Click For Summary
SUMMARY

The discussion centers on understanding polymorphism in object-oriented programming, specifically in Java. The example provided illustrates how class D, which extends class B, demonstrates polymorphism through method f() by invoking the overridden method g() in class E. The confusion arises regarding why method g() itself does not exemplify polymorphism, as it directly calls the superclass method. The key takeaway is that polymorphism is characterized by the ability of a method to exhibit different behaviors based on the object type invoking it.

PREREQUISITES
  • Understanding of Java inheritance and class structures
  • Familiarity with method overriding in Java
  • Knowledge of object-oriented programming principles
  • Basic comprehension of polymorphism in programming
NEXT STEPS
  • Study Java method overriding and its implications on polymorphism
  • Explore the concept of dynamic method dispatch in Java
  • Learn about interfaces in Java and their role in achieving polymorphism
  • Investigate real-world examples of polymorphism in software design patterns
USEFUL FOR

Students of computer science, Java developers, and anyone seeking to deepen their understanding of object-oriented programming concepts, particularly polymorphism.

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 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
11
Views
2K
Replies
1
Views
3K