-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance1.java
More file actions
59 lines (46 loc) · 953 Bytes
/
Copy pathInheritance1.java
File metadata and controls
59 lines (46 loc) · 953 Bytes
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
50
51
52
53
54
55
56
57
58
59
/*
Question:
Create a Bird class using inheritance.
- Animal class has a walk() method.
- Bird class extends Animal and has:
1. fly() method
2. sing() method
Print:
I am walking
I am flying
I am singing
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
// Parent class
class Animal {
// Method to walk
void walk() {
System.out.println("I am walking");
}
}
// Child class inheriting Animal
class Bird extends Animal {
// Method to fly
void fly() {
System.out.println("I am flying");
}
// Method to sing
void sing() {
System.out.println("I am singing");
}
}
public class Solution {
public static void main(String[] args) {
// Create Bird object
Bird bird = new Bird();
// Call inherited method
bird.walk();
// Call Bird methods
bird.fly();
bird.sing();
}
}