forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCat.java
More file actions
51 lines (30 loc) · 1.01 KB
/
Cat.java
File metadata and controls
51 lines (30 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
48
49
50
51
// Cat is a Subclass of Animal
// You create subclasses with the extends keyword
// Now Cat has all the Methods and Fields that Animal defined
// This is known as inheritance because Cat inherits all
// the methods and fields defined in Animal
public class Cat extends Animal{
// You can add new fields to the subclass
public String favToy = "Yarn";
// You can add new methods
public void playWith(){
System.out.println("Yeah " + favToy);
}
// Here I overrode the Animal walkAround method
public void walkAround(){
// this refers to a specific object created of type Cat
System.out.println(this.getName() + " stalks around and then sleeps");
}
public String getToy(){
return this.favToy;
}
public Cat(){
}
public Cat(String name, String favFood, String favToy){
// super calls the constructor for the super class Animal
super(name, favFood);
// We set the favToy value in Cat because it doesn't
// exist in the Animal class
this.favToy = favToy;
}
}