Abstract Class and Interface

Abstract Class and Interface

Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can’t be instantiated.

Let’s discuss the differences between abstract class and interface that are given below.

Abstract Class:

  • An abstract class is a class that contains at least one abstract method. The abstract method is a function declaration without anybody and it has the only name of the method and its parameters.
  • There can be any number of methods in the class and we have to declare the class as abstract only when there is an abstract method.

An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

Notes on Abstract class:

  • Objects cannot be created for abstract classes.
  • If a class has only one method as an abstract, then that class must be an abstract class. *The child class which extends an abstract class must define all the methods of the abstract class.
  • If the abstract method is defined as protected in the parent class, the function implementation must be defined as either protected or public, but not private.
  • The signatures of the methods must match, optional parameters given in the child class will not be accepted and error will be shown.
    Abstract classes that declare all their methods as abstract are not interfaces with different names. One can implement multiple interfaces, but not extend multiple classes (or abstract classes).

Interface:

An Interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class subclassing from it, it is said to implement that interface.

Rules of Interfaces:

  • All methods declared in an interface must be public; this is the nature of an interface.
  • All methods in the interface must be implemented within a class; failure to do so will result in a fatal error.
  • The class implementing the interface must use the exact same method signatures as are defined in the interface
  • Interfaces can be extended like classes using the extends operator.

Note on Interfaces:

  • We cannot create objects for the interface, but the class implementing the interface can have objects.
  • We cannot define a variable in an interface.
  • If we extend the interface all the methods of the interface must be implemented in the child class.

Abstract class vs Interface

Abstract classInterface
It can have constants, members, and method stubs (methods without a body), methodsIt can only have constants and method stubs.
Methods and members can have public or protected visibilityMethods of the interface should only be public not any other visibility
The concept of multiple inheritances is not supportedAn interface can extend or a class can implement multiple other interfaces.
The child class must implement all the abstract methods of the parent class when extend keyword is used.No need of implementing methods from the parent interface when the interface is extending another interface
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments