Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
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.
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.
The table below lists out the key differences between abstract class and interface.
Parameter | Abstract Class | Interface |
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?
Consider using abstract classes in the following cases:
Consider using interfaces in the following cases:
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co