28 lines
798 B
Java
28 lines
798 B
Java
/**
|
|
* 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();
|
|
}
|
|
}
|