-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.java
More file actions
38 lines (32 loc) · 1.08 KB
/
Runtime.java
File metadata and controls
38 lines (32 loc) · 1.08 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
/*
* Method Overriding: A subclass can provide a specific implementation of a method that is already defined in its superclass.
* super Keyword: Used to refer to the immediate superclass object. It can be used to access superclass methods and constructors.
* Constructor Chaining: When a subclass constructor is invoked, it implicitly calls the superclass constructor, forming a chain
* of constructors from the subclass to the topmost superclass.
*/
package Polymorphism;
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
void display() {
System.out.println("This is an animal");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the constructor of Animal
System.out.println("Dog constructor called");
}
@Override
void display() {
super.display(); // Calls display() method of Animal
System.out.println("This is a dog");
}
}
public class Runtime {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.display();
}
}