When we can use abstract class & interface? Why we are using abstract class & interface?

 

When to use Abstract Class?

  • An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
  • An abstract class is also good if we want to declare non-public members. 
  • In an interface, all methods must be public.
  • If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.

When to use interface?

  • If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.
  • Interfaces are a good choice when we think that the API will not change for a while.
  • Interfaces are also good when we want to have something similar to multiple inheritances since we can implement multiple interfaces.
  • If we are designing small, concise bits of functionality, use interfaces. If we are designing large functional units, use an abstract class.

Why we use abstract class & Interface?

  • An abstract class allows you to create functionality that subclasses can implement or override.
  • An interface only allows you to define functionality, not implement it. 
  • And whereas a class can extend only one abstract class, 
  • it can take advantage of multiple interfaces.


Comments

Popular posts from this blog

Basic Terminology

What are the main differences between INNODB and MYISAM?

What is normalization in DBMS?