-
Notifications
You must be signed in to change notification settings - Fork 2
/
Overriding.java
37 lines (31 loc) · 1.03 KB
/
Overriding.java
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
// Understanding Overriding
// Overriding is a concept in Java where a subclass can provide a specific implementation of a method that is already provided by its superclass.
// Method overriding is a dynamic/runtime polymorphism.
// The methods must have the same name, same parameters, and same return type.
// The methods can have different access modifiers.
// The methods can throw different exceptions.
class Person {
public void display() {
System.out.println("I am in the Person Class");
}
}
class Student extends Person {
public void display() {
System.out.println("I am in the Student Class");
}
}
class Employee extends Person {
public void display() {
System.out.println("I am in the Employee Class");
}
}
public class Overriding {
public static void main(String[] args) {
Person person = new Person();
Student student = new Student();
Employee employee = new Employee();
person.display();
student.display();
employee.display();
}
}