+ 3

What is the use of this abstract method? If it doesn't do anything, why not just defining it in the subclasses?

9th Aug 2016, 4:52 PM
Oliver Kratz-Lieber
Oliver Kratz-Lieber - avatar
4 Réponses
+ 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.
22nd Aug 2016, 2:37 PM
Frostbyte
0
simetimes they are helpful/often when you make complex programs where you want a method deckared but without conntent.
10th Aug 2016, 11:08 AM
Eric Zeus
Eric Zeus - avatar
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.
11th Aug 2016, 9:53 PM
Ruben Martinez Cabello
Ruben Martinez Cabello - avatar
0
It provides a prototype for what kind of methods can be present in the subclass
15th Aug 2016, 7:13 AM
Arth Dh
Arth Dh - avatar