-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaPracticeQuestionsonAbstractClassesAndnterfaces.java
70 lines (58 loc) · 1.39 KB
/
JavaPracticeQuestionsonAbstractClassesAndnterfaces.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
abstract class Pen{
abstract void write();
abstract void refill();
}
class FountainPen extends Pen {
void write(){
System.out.println("Write");
}
void refill() {
System.out.println("refill");
}
void changeNib(){
System.out.println("Changing the nib");
}
}
class Monkey{
void jump(){
System.out.println("Jumping...");
}
void bite(){
System.out.println("Biting...");
}
}
interface BasicAnimal{
void eat();
void sleep();
}
class Human extends Monkey implements BasicAnimal{
void speak(){
System.out.println("Hello sir!");
}
@Override
public void eat() {
System.out.println("Eating");
}
@Override
public void sleep() {
System.out.println("Sleeping");
}
}
class JavaPracticeQuestionsonAbstractClassesAndnterfaces{
public static void main(String[] args) {
FountainPen pen = new FountainPen();
pen.changeNib();
// Q3
Human Ashvin = new Human();
Ashvin.sleep();
// Q5
Monkey m1 = new Human();
m1.jump();
m1.bite();
// m1.speak(); --> Cannot use speak method because the reference is monkey which does not have speak method
BasicAnimal lovish = new Human();
// lovish.speak(); --> error
lovish.eat();
lovish.sleep();
}
}