45 lines
1.4 KiB
Java
45 lines
1.4 KiB
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.
|
|
*
|
|
* # Question 5
|
|
*
|
|
* Q: Where are the name and weight of the bird stored? How?
|
|
*
|
|
* A: The Bird class has a private instance variable name and weight which are
|
|
* inherited from the Animal class.
|
|
*
|
|
* Q: Suppose a student has a bird as a pet. Can the student get the altitude of
|
|
* his pet?
|
|
*
|
|
* A: No. The student's pet is an Animal object. The Bird object is a subclass
|
|
* of Animal. getAltitude is only avaliable in Animal object. When we assign a
|
|
* Bird to an Animal variable, upcasting happend and only attributes and methods
|
|
* in the super class can be accessed.
|
|
*
|
|
*/
|
|
public class Start {
|
|
public static void main(String[] args) {
|
|
Cat.testCat();
|
|
Dog.testDog();
|
|
Student.testStudent();
|
|
Animal.testAnimal();
|
|
Bird.testBird();
|
|
Chicken.testChicken();
|
|
}
|
|
}
|