-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolyMorphism.java
More file actions
49 lines (45 loc) · 1.01 KB
/
PolyMorphism.java
File metadata and controls
49 lines (45 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.oopspackage;
// 🔹 What is Polymorphism?
// Polymorphism means “many forms”.
// 👉 Same method name, but different behavior.
// ✅ Types of Polymorphism (Java)
// Compile-time Polymorphism → Method Overloading
// Run-time Polymorphism → Method Overriding
public class PolyMorphism {
public static class Dog{
void speak(){
System.out.println("Bhau Bhau");
}
}
public static class Cat{
void speak(){
System.out.println("Meow Meow");
}
}
public static class Lion{
void speak(){
System.out.println("Grrrrr");
}
}
public static class Pikachu{
void speak(){
System.out.println("Pika Pika");
}
}
public static class Human{
void speak(){
System.out.println("Hello");
}
}
public static void main(String[] args) {
Dog tommy = new Dog();
Cat c = new Cat();
Lion l = new Lion();
Pikachu p = new Pikachu();
Human h = new Human();
tommy.speak();
c.speak();
p.speak();
h.speak();
}
}