Technical SpeaksCodeForDevelopersThe official blog of Technical Speaks
Core Java

What is Polymorphism in Java | Java Polymorphism | Polymorphism

By Saurabh K · Published · Updated · 1 min read

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:

code
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:

Written by

Saurabh Kumar

Founder of Technical Speaks and Top Rated Upwork SEO expert with 10+ years of hands-on experience.

Part of the Technical Speaks team.

Comments

Leave a comment