Abstract Classes
The word "abstract" means "An object which does not exist at any particular time or place, but rather exists as a type of thing (as an idea, or abstraction). In philosophy, an important distinction is whether an object is considered abstract or concrete. Abstract Classes in OOPs are those classes that do not represent anything real.
- Still it may make sense to create such a class.
- All derived classes will inherit its code, thus the code has to be written only once.
- We call a class that we cannot create objects of Abstract.
- For example, there is no concrete vehicle, thus the class vehicle will be abstract.
- Concrete derived classes, such as Car or Motorcycle may inherit the code for wheels, seats, engines, ...
- Another reason is to get consistent behavior for same things - A wheel will thus always behave the same.
- Often most of the work goes into the abstract parent class.
Abstract Methods
An abstract method has no body or definition in the parent class, the child class must provide definitions for the abstract methods.
- An abstract class may not know how a method can be implemented.
- Still that method must be there
- An Abstract Method is declared in a base class and must be implemented by all classes derived from it.
- Why? The (abstract) base class represents the concept while the derived classes are “only” the implementations.
0 comments:
Post a Comment