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