Polymorphism in java

 Polymorphism in java


Polymorphism is a mechanism in Java that allows objects of different types to be treated as if they are of the same type. Here is an example of a method that demonstrates polymorphism:



public void printName(Person person) {

  System.out.println(person.getName());

}

In this example, we define a method called printName that takes a Person object as a parameter. The method calls the getName method of the Person object to print the person's name. This method can be called with a Person object or any object that extends the Person class (such as a Student object).

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...