Tuesday, May 29, 2018

What’s an ABSTRACT CLASS? Why and when should we use an abstract class?

What’s an ABSTRACT CLASS?
Similar to interfaces but cannot be instantiated, and are frequently either partially implemented, or not at all implemented. 
Different from interface in that:
  • A class can inherit from only one abstract class but can implement an unlimited number of  interfaces.
  • Abstract classes DO contain code & data.
  • You can specify methods as ‘virtual’ to force derived classes to create its own implementation.
Problems without abstract class:
  • Duplication of code. So, code maintainability will be big issue.
  • With implementation of concrete (non-abstract) BASE CLASS, developer can instantiate and use the base class as actual classes, which is not intended to do.
So, in short, we should create an ABSTRACT CLASS, 
  1. When want to move the common functionality of 2 or more related classes into a base class and 
  2. When we don't want the base class to be instantiated.

No comments:

Post a Comment