-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPracticeCode.java
95 lines (73 loc) · 3.07 KB
/
PracticeCode.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
92
93
94
95
/**************************************************************************
* The class name JavaFiddle is used to allow it to be recognized at https://javafiddle.leaningtech.com/
* This will be the main driving class to test all the other classes
***************************************************************************/
public class JavaFiddle {
public static void main(String[] args) {
Student bob = new Student("bobisdabest");
Student george = new Student("gijoe25");
Student jill = new Student("upthehill234", "Junior");
// Add your own Teacher class for this code to work!
//Teacher profSnape = new Teacher("snapesbackintown");
//Users array can hold any classes derrived from User.
//Create a Teacher class and give it a try!
User[] users = new User[] {bob, george, jill};
for(int i = 0; i < users.length; i++) {
displayUserType(users[i]);
}
}
public static void displayUserType(User user) {
//instanceof is a keyword that lets us know what type of
//object 'user' is. This is a handy way to custimize functionality
//based off of the type of object.
if(user instanceof Teacher) {
System.out.println(user.getUsername() + " is a teacher.");
}
}
}
/***************************************************************
* This is the User abstract class.
***************************************************************/
public abstract class User{
private String username;
User() {
username = "unknown";
}
User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}
/***************************************************************
* This is the Student class, which inherits from User
***************************************************************/
public class Student extends User {
//inherits username variable from user
//create a variable unique to Student
private String currentClass;
Student(String username) {
super(username);
//calls user's constructor, and passes it the username
}
Student(String username, String currentClass) {
super(username);
//The "this" keyword refers to the member variable called currentClass inside of this class.
//Because I am using the same name for the variable I am passing into the constructor,
//I need to use the "this" keyword to differentiate between the two.
this.currentClass = currentClass;
}
//inherits the getUsername method from User
//add a method just to Student called getCurrentClass
public String getCurrentClass() {
return currentClass;
}
}
/*****************************************************
* To-Do: Practice Inheritance
* Create a new class called Teacher, that inherits
* from User, and adds its own unique data and methods.
* Hint: Examine how the Student class inherits from User,
* and use that as an example.
******************************************************/