I just started learning Java and I am confused about creating a new object when I have a child class extends its parent class and/or also implements a interface.
For example, if I have:
Parent class: Animal class with methods walk, eat, sleep Child class: Bird class with a method sing Interface: Flyable with a method fly
And "Bird extends Animal implements Flyable" or just "Bird extends Animal"
So when I create new objects, I can have:
- Animal bird1 = new Animal();
- Animal bird2 = new Bird();
- Bird bird3 = new Bird();
- Flyable bird4 = new Bird();
As I understand it (if I didn't misunderstand anything...), unless I cast the object when calling the methods, for 1. and 2. I can call all the methods in Animal; for 3. I can call every method in Animal, Bird, and/or Flyable(if I implemented it); for 4. I can only call the method in Flyable.
So my question is, why not we only create 3. in Java because we can use all the methods in parent class and child class, why and when do we need 1. 2. and 4.?
Thanks for the help!