QA's approach 2 Java - Abstract Class vs Interface Theory

Abstract Class vs Interface, Differences, when to use? Why not create an object?

Interface

Interface is used when you want to define a contract and you don't know anything about implementation. (here it is total abstraction as you don't know anything).

Interface is used in a situation,

1. When you know the contract methods but don't know anything about the implementation.

2. Your contract implementation can change in future.

3. You want to achieve dynamic polymorphism and loose coupling that is by just changing one line of code, you should be able to switch between contract implementer.

4. Since multiple inheritance is not supported in Java, you cannot inherit your class from two abstract classes. An interface is your only option in such situations.

5. If there is no default or common behavior among all the classes that are inheriting from abstract class then interface may be a better choice.

Example Analogy for point 1:

Redbus interface lists out all the availability from various service providers, where how the availability is retrieved is unknown by the Redbus & how the booking is implemented is unknown.
What is known for RedBus is method Names which are used for retrieving the availability & booking a reservation.

So the abstract methods acts as contract between RedBus and the Service providers who actual provide the Bus services.

Example Analogy for point 2 & 3:

Assume today a Company providing service to Restaurants is using "ABC" payments Gateway, as the days passed, the same Company has its own Payment Gateway. Now without even worrying the Restaurant owners who are using this Companies services, they can plug in their own Payments Gateway & replace the third party service, if it is loosely coupled.

Abstract Class:

Abstract class is used when you know something and rely on others for what you don't know.(here it is partial abstraction as some of the things you know and some you don't know.

Abstract class is used in a situation:

1. Use abstract class if you have a default implementation of some behaviour that child classes do not have to implement

2. Prefer abstract classes if your contract has a possibility of changing over time. So if you are using an abstract class and need to add a new method to your abstract class, you can happily add that without breaking any code using that class. The same is not true for interfaces.
Example Analogy:

Assume a Marriage Bureau which has its site hosted & also provides complete end-end marriage services other than just profile matching, for which they in turn utilizes some vendor services.
Now Marriage Bureau knows the Profile matching business implementations, but not aware of the Party organizing service, Food catering service, etc. What is only know to Bureau providers is the contract methods which needs to be used to achieve the Party organizing & Food catering services.

Differences between abstract class vs Interface

1. abstract keyword is used to create an abstract class and it can be used with methods also whereas interface keyword is used to create interface and it can’t be used with methods.

2. Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface.

3. Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.

4. Abstract classes can have constructors but interfaces can’t have constructors.

5. Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use abstract keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations.

6. Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.

7. A subclass can extend only one abstract class but it can implement multiple interfaces.

8. Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.

9. We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.

10. Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.

when should we use Interface over Abstract class

Interface or Abstract Class

Whether to chose between Interface or abstract class for providing contract for subclasses is a design decision and depends on many factors, lets see when Interfaces are best choice and when can we use abstract classes.

Java doesn’t support multiple class level inheritance, so every class can extend only one superclass. But a class can implement multiple interfaces. So most of the times Interfaces are a good choice for providing base for class hierarchy and contract. Also coding in terms of interfaces is one of the best practices for coding in java.

If there are a lot of methods in the contract, then abstract class is more useful because we can provide default implementation for some of the methods that are common for all the subclasses. Also if subclasses don’t need to implement particular method, they can avoid providing the implementation but in case of interface, the subclass will have to provide implementation for all the methods even though it’s of no use and implementation is just empty block.

If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.

Use Abstract classes and Interface both

Actually most of the times, using Interfaces and abstract classes together is the best approach for designing a system, for example in JDK java.util.List is an interface that contains a lot of methods, so there is an abstract class java.util.AbstractList that provides skeletal implementation for all the methods of List interface so that any subclass can extend this class and implement only required methods.

We should always start with an interface as base and define methods that every subclasses should implement and then if there are some methods that only certain subclass should implement, we can extend the base interface and create a new interface with those methods. The subclasses will have option to chose between the base interface or the child interface to implement according to its requirements. If the number of methods grows a lot, its not a bad idea to provide a skeletal abstract class implementing the child interface and providing flexibility to the subclasses to choose between interface and abstract class.

Why couldn't we create an object for Interface or Abstract class:

The problem with creation of object for interface or abstract class is that JVM is not able to identify how much memory it has to allocate to unimplemented methods.


Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?