-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractDemo1.java
91 lines (73 loc) · 2.06 KB
/
AbstractDemo1.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
87
88
89
90
91
package Basics;
// https://www.youtube.com/watch?v=6cORJLJQ1KY&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=342
/**
* Abstract
*
* <p>1) Can decorate - class - method
*
* <p>2) abstract class - CAN NOT BE instantiated - STILL NEED CONSTRUCTOR (used in sub class) - in
* development, we always offer sub class that can instantiate via above
*
* <p>3) abstract method - ONLY has method declare, has NO code body (method implementation) -
* inference : if a class has abstract method -> this class must be an ABSTRACT CLASS - Abstract
* class can has NO abstract method - if sub class overwrites all abstract methods in super class ->
* sub class CAN instantiate - if sub class NOT overwrites all abstract methods in super class ->
* sub class CAN NOT instantiate, this sub class is also an abstract class
*/
public class AbstractDemo1 {
public static void main(String[] args) {
// run
// wrong : 'Person3' is abstract; cannot be instantiated
// Person3 p1 = new Person3();
// p1.eat();
Student3 s1 = new Student3();
s1.eat();
s1.study();
}
}
abstract class Creature {
public abstract void breath();
}
abstract class Person3 extends Creature {
// attr
String name;
int age;
// constructor -> can be used in subclass
public Person3() {}
public Person3(String name, int age) {
this.name = name;
this.age = age;
}
// method
// below is NOT an abstract method
public void _eat() {}
// below is an abstract method
public abstract void eat();
public void walk() {
System.out.println("person walk !!!");
}
}
class Student3 extends Person3 {
// attr
// constructor
public Student3() {
super();
}
public Student3(String name, int age) {
super(name, age);
}
// method
@Override
public void eat() {
System.out.println("Student3 eat !!!!!");
}
public void study() {
System.out.println("Student3 study !!!!!");
}
@Override
public void breath() {
System.out.println("Student3 breath !!!!!");
}
}
// also an abstract class
abstract class Student3_abs extends Person3 {}