-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractInterfaceStuff.java
More file actions
94 lines (74 loc) · 2 KB
/
AbstractInterfaceStuff.java
File metadata and controls
94 lines (74 loc) · 2 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
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
87
88
89
90
91
92
93
94
package java;
public class AbstractInterfaceStuff {
public static void main(String[] args) {
// HelloInterface hi = new GreeterAbstract(); // throws error "Cannot instantiate the type GreeterAbstract"
HelloInterface hi = new HelloClass();
hi.sayHello();
GreeterAbstract ga = new HelloWorldClass();
ga.sayHelloName();
ga = new HelloWorldClass("Bob");
ga.sayHelloName();
String foobar = ga.foobar();
System.out.println("foobar: " + foobar);
}
}
interface HelloInterface {
// String type; // illegal, need to provide a value
abstract void sayHello();
abstract void sayHello(String name);
}
class HelloClass implements HelloInterface {
@Override
public void sayHello() {
System.out.println("Hello!");
}
@Override
public void sayHello(String name) {
System.out.println("Hello " + name + "!");
}
}
interface FoobarInterface {
abstract String foobar();
}
abstract class GreeterAbstract implements HelloInterface, FoobarInterface {
private String defaultName = "Alice";
String name;
public GreeterAbstract() {
this.name = defaultName;
}
public GreeterAbstract(String name) {
this.name = name;
}
@Override
public void sayHello() {
System.out.println("Hello!");
}
public void sayHelloName() {
System.out.println("Hello " + name);
}
@Override
public void sayHello(String name) {
System.out.println("Hello " + name + "!");
}
@Override
public String foobar() {
return "foobar";
}
abstract void sayHelloWorld();
}
class HelloWorldClass extends GreeterAbstract {
public HelloWorldClass() {
super();
}
public HelloWorldClass(String name) {
super(name);
}
@Override
public void sayHelloWorld() {
System.out.println("Hello world!");
}
@Override
public void sayHelloName() {
System.out.println("Hello " + name + "!");
}
}