+ 3
What is the use of this abstract method? If it doesn't do anything, why not just defining it in the subclasses?
4 Respostas
+ 5
Think of the animal, dog and cat example from a few chapters back. Animal is an abstract class and can't be initiated, because there is no "Animal"-Animal. It could provide abstract methods like makeSound(). Subclasses like dog and cat will need to define a body for makeSound() like
{System.out.print("Woof");}
It ensures that every subclass of Animal implements this method, so you could see it as a blueprint for further classes.
0
simetimes they are helpful/often when you make complex programs where you want a method deckared but without conntent.
0
Because you may want to use a method that you will define later in a child class, inside a method of the parent class, while not allowing to use the class vehicle abstraction directly
ex: class vehicle with method accelerate, brake, turn and drive2address. method drive2address uses accelerate, brake and turn to reach the destination. accelerate brake and turn are abstract methods.
class car extends vehicle, redefines the abstract methods, which are different from class bike (pedaling), class boat (rowing) or others.
you could simply define the methods as empty in vehicle class and redefine them on children, but the language wouldnt check that these methods are all redefined.
0
It provides a prototype for what kind of methods can be present in the subclass