Explain with example to describe when to use abstract class and interface?


Consider a scenario where all Cars will have 4 tyres and other features can be different.
In this case any subclass of Car has to have 4 tyres. This is a case where abstract class will be used and a default implementaion for tyres will be provided.

public abstract class Car

{

   public abstract String getCarName();

   public final int getNoOfTyres()
   {
     return 4;
   }

}
Consider a scenario where Cars can have any number of tyres and other features can also be different. In this case interface will be created.
public interface Car
{

 public abstract String getCarName();
 public abstract int getNoOfTyres();
 
}


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email