0

Can anyone explain how to use an abstract class?

21st May 2017, 7:05 AM
DoubleVermine
DoubleVermine - avatar
2 Answers
+ 2
Absrtract classes cannot be instantiated. An abstract class is great to allow sub-classes to utilize the base classes functionality. "The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share." Take a look at my Pirate Wars OOP code for a practical example. The abstract class is the Pirate and Ally class. https://code.sololearn.com/cZKcrU0TrMtX/?ref=app
21st May 2017, 3:45 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
What @Rrestoring faith said is correct. The abstract keyword is best used on classes who will never be instantiated. For example: I am making a game with a player, NPCs, and enemies. Instead of creating 3 classes (one for player, NPC, and enemy), I would create 4 classes. I would have a "player" class, an "NPC" class, an "enemy" class, and an abstract "character" class. In the Character class, I would have variables such as health, name, texture, position, etc, etc. Then when you want to create the other 3 classes, you can have them all inherit these values from the Character class. In short: abstract classes are never instantiated and are only used to be a general parent class for other, more specific classes. I hope I could help.
21st May 2017, 4:52 PM
Ethan
Ethan - avatar