Difference between Abstract class and Interface in Java

UPDATED: 03 February 2015

One of the popular question asked in every single Java interview. People always gets confuse for giving the answer as they don't have fundamental clearance about the topic. If you explain difference with example then interviewer thinks that you have concept clear in your mind. Lets first understand with example and afterword we'll check other characteristics.

Example
Consider a real world object simple mobile phone.
Interface: Each mobile must provide Calling and SMS facility so we can create Interface that has methods Calling, SMS, etc... So if any class Implements MobileInterface then it has to write implementation part of all method written MobileInterface. In our example its Calling ans SMS. Check following source code for better understanding.

Abstract Class: Now What other you can think of that mobile, it may have Mp3 Player, GPS, Camera, etc... but its not compulsory for mobile to have all this feature so we can create an Abstract class that has methods like Mp3Player, GPS, Camera, etc... Its not compulsory to implement all methods of SmartPhoneAbstractClass Abstract class. Check following source code for better understanding.

MobileInterface Source Code
public interface MobileInterface {
    /* Each mobile must have calling facility */
    public void call();
    /* Each mobile must have sms facility */
    public void sms();
}

SmartPhoneAbstractClass Source Code
public abstract class SmartPhoneAbstractClass {
   
    /* Its not compulsory to have MP3 Player in each mobile */
    public void mp3Player(){
        System.out.println("Can have implementation");
    }
    
    /* Its not compulsory to have GPS in each mobile */
    public void GPS(){
        System.out.println("Can have implementation");
    }
}

Main Source Code
public class Main extends SmartPhoneAbstractClass implements MobileInterface{

    /* Mobile must provide calling service */
    @Override
    public void call() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /* Mobile must provide SMS service */
    @Override
    public void sms() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /* Simple mobile has MP3 Player */
    @Override
    public void mp3Player() {
        super.mp3Player();
    }
}

Now you have clear idea about between Interface and Abstract Class. Lets check other characteristics Interface and Abstract Class.

  • Interface doesn't contain implementation part(only declaration) where Abstract class has implementation part.
  • implements keyword is used to implement an Interface where extends keyword is used to extend an Abstract class.
  • You can implement multiple Interface where you can extend only one Abstract class.
  • Variable declared in Interface are by default final where Abstract class can have non final variables.
  • Members of Interface are by default public where Abstract class can have private, protected members.
  • Interface can't have main method where Abstract class can have main method.
  • Interface can't have default constructor where Abstract class can have default constructor.
  • Interface is looks like Java class but its different type of Java file. 
  • You can't create instance of Abstract class. 

Java 8
Functional Interface in Java 8
default and static method in Java Interface

0 comments :