-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36_SuperKeyword
More file actions
49 lines (41 loc) · 1.01 KB
/
36_SuperKeyword
File metadata and controls
49 lines (41 loc) · 1.01 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
// super = keyword refers to the superclass (parent) of an object
// very similar to the "this" keyword
<Main.java>
public class Main {
public static void main(String[] args) {
Hero hero1 = new Hero("Batman",42,"$$$");
Hero hero2 = new Hero("Superman",43,"everything");
System.out.println(hero2.toString());
/* >>Superman
43
everything
*/
/* System.out.println(hero1.name); //>>Batman
System.out.println(hero1.age); //>>42
System.out.println(hero1.power); //>>$$$
*/
}
}
<Person.java>
public class Person {
String name;
int age;
Person(String name, int age){
this.name = name;
this.age=age;
}
public String toString(){
return this.name + "\n" + this.age+"\n";
}
}
<Hero.java>
public class Hero extends Person{
String power;
Hero(String name, int age, String power){
super(name, age);
this.power = power;
}
public String toString() {
return super.toString() +this.power;
}
}