Difference Between Abstract Class and Interface

Abstract Class vs Interface

Abstract class and Interface are two object oriented constructs found in many object oriented programming languages like Java. Abstract class can be considered as an abstract version of a regular (concrete) class, while an interface can be considered as a means of implementing a contract. Abstract class is a class that cannot be initialized but can be extended. Interface is a type that has to be implemented by other classes. In Java, Abstract classes are declared using Abstract keyword, while the interface keyword is used to define an interface.

What is an Abstract Class?

Typically, Abstract classes, also known as Abstract Base Classes (ABC), cannot be instantiated (an instance of that class cannot be created). So, Abstract classes are only meaningful to have if the programming language supports inheritance (ability to create subclasses from extending a class). Abstract classes usually represent an abstract concept or entity with partial or no implementation. Therefore, Abstract classes act as parent classes from which child classes are derived so that the child class will share the incomplete features of the parent class and functionality can be added to complete them.

Abstract classes may contain Abstract methods. Subclasses extending an abstract class may implement these (inherited) Abstract methods. If the child class implements all such Abstract methods, it is a concrete class. But if it does not, the child class also becomes an Abstract class. What all this means is that, when the programmer nominates a class as an Abstract, she is saying that the class will be incomplete and it will have elements that need to be completed by the inheriting subclasses. This is a nice way to create a contract between two programmers, which simplifies tasks in software development. The programmer, who writes code to inherit, needs to follow the method definitions exactly (but of course can have her own implementation).

What is an Interface?

An interface is an abstract type that is used to specify a contract that should be implemented by classes, which implement that interface. The interface keyword is used to define an interface and Implements keyword is used for implementing an interface by a class (in Java programming language). Usually, an interface will only contain method signatures and constant declarations. Any interface that implements a particular interface should implement all methods defined in the interface, or should be declared as an abstract class. In Java, the type of an object reference can be defined as an interface type. But that object must either be null or should hold an object of a class, which implements that particular interface. Using Implements keyword in Java, you can implement multiple interfaces to a single class.

What is the difference between Abstract Class and Interface?

Abstract classes usually represent an abstract concept or an entity with partial or no implementation. On the other hand, an interface is an abstract type that is used to specify a contract that should be implemented by classes. Abstract classes should be inherited (or extended), while interfaces should be implemented. Abstract classes may contain abstract methods, whereas an interface should only contain abstract methods. Abstract classes can contain any variables, but Interfaces can only define constants. A class cannot inherit from more than one abstract class but can implement multiple interfaces. An Interface cannot implement another interface. However an interface can extend a class.

ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau26twayrq5mTqXqkuMCsqmaZnpl6t7%2BMoqWtnaKbrqSxjg%3D%3D