-
Notifications
You must be signed in to change notification settings - Fork 4
/
Java_Inheritance_I.java
86 lines (67 loc) · 1.89 KB
/
Java_Inheritance_I.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
___________________________________________________________________Problem Statement_____________________________________________________
problem Statement link: hackerrank.com/challenges/java-inheritance-1/problem
Using inheritance, one class can acquire the properties of others. Consider the following Animal class:
class Animal{
void walk(){
System.out.println("I am walking");
}
}
This class has only one method, walk. Next, we want to create a Bird class that also has a fly method. We do this using extends keyword:
class Bird extends Animal {
void fly() {
System.out.println("I am flying");
}
}
Finally, we can create a Bird object that can both fly and walk.
public class Solution{
public static void main(String[] args){
Bird bird = new Bird();
bird.walk();
bird.fly();
}
}
The above code will print:
I am walking
I am flying
This means that a Bird object has all the properties that an Animal object has, as well as some additional unique properties.
The code above is provided for you in your editor. You must add a sing method to the Bird class, then modify the main method accordingly so that the code prints the following lines:
I am walking
I am flying
I am singing
_________________________________________________________ Solution________________________________________________
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Animal{
void walk()
{
System.out.println("I am walking");
}
}
class Bird extends Animal
{
void fly()
{
System.out.println("I am flying");
}
void walk()
{
System.out.println("I am walking");
}
void sing()
{
System.out.println("I am singing");
}
}
public class Solution{
public static void main(String args[]){
Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();
}
}