This commit is contained in:
2022-03-19 17:47:52 +08:00
parent 6bf672bd5b
commit 1c8f95c4b3
35 changed files with 1597 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
/**
* Answer to question.
*
* # Question 4
*
* Q: Now that the Student class uses an animal as pet, can you still use a cat
* object as the pet of a student? Why or why not?
*
* A: Yes, you can. The Student class has a method getPet() that returns the pet
* which is Animal type.
*
* Q: Can you now use a dog object as the pet of a student? Why or why not?
*
* A: Yes, you can. But only attributes and methods definded in the Animal class
* can be accessed. When you do so, the Dog object will be converted to an
* Animal object. Assign a child object to a parent object is called upcasting
* in Java. Upcasting is implicit.
*
*/
public class Start {
public static void main(String[] args) {
Cat.testCat();
Dog.testDog();
Student.testStudent();
Animal.testAnimal();
}
}