Java/J2EE and SOA (252 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (46 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (252 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is the difference between Abstract Class and Interface in Java?

Published on Aug 08,2019 140 Views
78 / 88 Blog from Java Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Abstract classes and interfaces are the two main building blocks of the Java Programming Language. Though both are primarily used for abstraction, they are very different from each other and cannot be used interchangeably. In this article let’s figure out what is the difference between abstract class and interface in Java.

Topics discussed in this article are as follows:

To understand the differences between abstract class and interface in Java, you need to know what an abstract class is and what an interface is. So, let’s start by discussing what those are.

What is Abstract Class in Java?

In any programming language, abstraction means hiding the irrelevant details from the user to focus only on the essential details to increase efficiency thereby reducing complexity. In Java, abstraction is achieved using abstract classes. An abstract class captures common characteristics of subclasses and may or may not contain any abstract method. It cannot be instantiated but can be only used as a superclass by its subclasses. Here’s an example program demonstrating abstract class:

Note: An abstract method, is a method which is not implemented in place and adds incompleteness to class.

package MyPackage;
//abstract class
abstract class Animal{
	 String AnimalName = " "; 
     
	    Animal(String name) 
	    { 
	        this.AnimalName = name; 
	    } 
	      
	    // declare non-abstract methods 
	    // it has default implementation 
	    public void BasicInfo(String details) 
	    { 
	        System.out.println(this.AnimalName + " " + details); 
	    } 
	      
	    // abstract methods which will be 
	    // implemented by its subclass(es) 
	    abstract public void habitat(); 
	    abstract public void respiration(); 
	} 
class Terrestrial extends Animal 
{ 
   
      
    // constructor 
    Terrestrial(String name) 
    { 
        super(name);
    } 
      
    @Override
    public void habitat()  
    { 
        System.out.println("leave on land and");  
    } 
      
    @Override
    public void respiration()  
    { 
    	System.out.println("respire through lungs or trachea. ");  
    }
}
class Aquatic extends Animal 
{ 
      
    // constructor 
    Aquatic(String name) 
    { 
        super(name);
    } 
      
    @Override
    public void habitat()  
    { 
        System.out.println("It leaves in water and");  
    } 
      
    @Override
    public void respiration()  
    { 
    	System.out.println("respire through gills or their skin. ");  
    }
}


class AbstractClassDemo
{ 
    public static void main (String[] args)  
    { 
      
        // creating the Object of Terrestrial class 
        // and using Animal class reference. 
        Animal object1 = new Terrestrial("Humans"); 
        object1.BasicInfo("are terrestrial beings, they "); 
        object1.habitat(); 
        object1.respiration(); 
         
          
        System.out.println(" "); 
          
        // creating the Objects of circle class 
        Animal object2 = new Aquatic("Fishes"); 
        object2.BasicInfo("are aqautic beings, they "); 
        object2.habitat(); 
        object2.respiration(); 
        
    } 
} 

Output

Humans are terrestrial beings, they 
leave on land and
respire through lungs or trachea. 
 
Fishes are aqautic beings, they 
It leaves in water and
respire through gills or their skin. 

The BasicInfo() is a method shared by Terrestrial and Aquatic classes. Since Animal class can’t be initiated, we are creating the objects of Terrestrial and Aquatic classes for programming purpose. Next up, we have interfaces.

Interface in Java

Another way of achieving abstraction in Java is by using interfaces. An interface is a collection of abstract methods, it does not have any concrete methods, unlike an abstract class. But unlike abstract class, an interface provides full abstraction in Java. It can have both methods and variables just like a class. However, the methods declared in an interface are abstract by default. Here’s an example program demonstrating abstract class:

package MyPackage;

interface Animals{
	      
	    // abstract methods 
	    void habitat(); 
	    void respiration(); 
	} 
class TerrestrialA implements Animals
{ 
	String AnimalName = " "; 
	// constructor   
	TerrestrialA(String name) 
    { 
        this.AnimalName = name; 
    } 
 
  @Override
  public void habitat()  
  { 
      System.out.println(this.AnimalName + " leave on land and");  
  } 
    
  @Override
  public void respiration()  
  { 
  	System.out.println("respire through lungs or trachea. ");  
  }
}
class AquaticA implements Animals
{ 
	String AnimalName = " "; 
	// constructor   
	AquaticA(String name) 
    { 
        this.AnimalName = name; 
    } 
  @Override
  public void habitat()  
  { 
      System.out.println(this.AnimalName + " leave in water and");  
  } 
    
  @Override
  public void respiration()  
  { 
  	System.out.println("respire through gills or their skin. ");  
  }
}


class JavaInterfaceDemo
{ 
  public static void main (String[] args)  
  { 
    
      // creating the Object of Terrestrial class 
      // and using Animal class reference. 
      Animals object1 = new TerrestrialA("Humans"); 
      object1.habitat(); 
      object1.respiration(); 
       
        
      System.out.println(" "); 
        
      // creating the Objects of circle class 
      Animals object2 = new AquaticA("Fishes");  
      object2.habitat(); 
      object2.respiration(); 
      
  } 
} 

Output

Humans leave on land and
respire through lungs or trachea. 
 
Fishes leave in water and
respire through gills or their skin.  

If you do not have any common code between your classes, then you can go for the interface. An interface is more like a blueprint of a class since it does not have any non-abstract methods.

From the above content, you might have noticed the key difference between abstract class and interface in Java. Which is, unlike abstract class, an interface provides full abstraction in Java. Now let’s go ahead and list out other differences.

Abstract Class vs Interface 

The table below lists out the key differences between abstract class and interface.

ParameterAbstract ClassInterface

Default Method Implementation

It can have default method implementation

Interfaces provide pure abstraction & can not have implementation at all

Variables

It may contain non-final variables.

Variables declared in an interface are by default final

Keyword Used

An abstract class can be extended using the keyword “extends

The interface should be implemented using keyword ïmplements

Access Modifiers

Can  have public, protected, private and default modifier

Interface methods are by default public. you can not use any other access modifier with it

Speed of Implementation

It is faster than the interface

An Interface is somewhat slower & require extra indirection

Normal Class

It can extend only one abstract class

Can implement multiple interfaces

Constructors

An abstract class can have constructors

An interface can not have constructors

Multiple Inheritance

An abstract class can extend another class and can implement multiple Java interfaces

The interface can extend another Java interface only

Well, now you the key differences between abstract class and interface in Java. But, how do you decide when to use which among these two?

When to use Abstract Class & When to use Interface?

Consider using abstract classes in the following cases:

  • If you have some related classes that need to share the same lines of code
  • When you want to define the non-static or non-final fields
  • When there are methods or fields or require access modifiers other than public (such as protected and private)

Consider using interfaces in the following cases:

  • When you want to achieve pure abstraction
  • If you want to employ multiple inheritance, ie, implement more than one interface
  • When you want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

This brings us to the end of this article. I have covered one of the most frequently asked Java questions in Interview, which is the difference between abstract class and interface in Java.

Make sure you practice as much as possible and revert your experience.  

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this ‘java Map interface’ article and we will get back to you as soon as possible.

 

 

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.