Java Polymorphism

Polymorphism is one of the most important concept in OOPS and also It is beneficial for any other programming languages.

Made from two words:

1. “Poly” means “many”
2. “morphs” means “forms”

Polymorphism – A single action in different ways…

Same methods in different class to perform different tasks – Polymorphism…

Program:

class Subject{
public void subjectName() {
System.out.println(“This is Subject Name”);
}
}
class Physics extends Subject {
public void subjectName() {
System.out.println(“Subject is Physics”);
}
}
class Mathematics extends Subject{
public void subjectName() {
System.out.println(“Subject is Mathematics “);
}
}
class MyMainClass {
public static void main(String[] args) {
Subject mySubject = new Subject();
Subject myPhysics = new Physics();
Subject myMathematics = new Mathematics();
mySubject.subjectName();
myPhysics.subjectName();
myMathematics.subjectName();
}
}

Output:

This is Subject Name

Subject is Physics

Subject is Mathematics

Connect with us: