lab5
This commit is contained in:
27
lab/lab5/Question4/Start.java
Normal file
27
lab/lab5/Question4/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user