Wednesday, June 15, 2011

Abstract Classes

Abstract classes involve the use of a common base class when you want to leave certain details
up to its inheritors—specifically, when you need to create a foundational object whose methods
are not fully defined. You will find that by using abstraction, you can create very extensible
architecture within your development projects.
For example, file-format parsing lends itself particularly well to the abstract approach. In
this case, you know that the object will need a set of methods, like getData() or getCreatedDate(),
in order for it to interoperate with other classes; however, you want to leave the parsing methods
up to inheriting classes that are designed for a specific file format. By using abstract classes,
you can define that a parse() method must exist, without needing to specify how it should
work. You can place this abstract requirement and the fully defined methods in a single class
for easier implementation.
You might think of abstract classes as partial classes because they do not define the implementation
for all the methods they declare. Instead of implementing all methods, an abstract
class has the added ability to define abstract methods, which are method prototypes lacking a
body. These methods will be implemented when the class is derived. However, an abstract class
doesn’t need to consist solely of abstract methods; you’re free to declare fully defined methods
as well.

No comments:

Post a Comment