Posts

Showing posts with the label abstract class

Difference between an abstract class and interface in Java

Another commonly asked interview question is to explain the difference between an abstract class and an interface. So in this blog post, I will give you a detailed explanation. What is an Abstract Class? An abstract class is a class that has one or more abstract method. An abstract method is a method that has the “abstract” keyword specified in the method definition. It does not have any method body, it is up to the subclass of the abstract class to provide an implementation for the abstract method. So why is an abstract class useful? Sometimes, you will want to create a super class that defines a method that all its sub classes must implement, but it leaves the exact implementation to the sub class. For ex. suppose you have a class called Shape, you can define an abstract method called draw. You can then have sub-classes for various shapes like Rectangle, Triangle, etc which provide implementations for the draw method. So while the “draw” method makes no sense ...