- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Abstract Class and Interface in Java
Following are the notable differences between interface and abstract class in Java.
Abstract Class | Interface |
---|---|
An abstract class may contain concrete method. | All the methods of an interface are abstract. |
To use an abstract class, you need to inherit it. Provide body to (override) the abstract methods if there are any. | To use an interface you need to implement the interface and provide body to (override) all the abstract methods of it. |
Members of an abstract class can be public, private, protected or default. | All the members of the interface are public by default. |
Example
public class Tester { public static void main(String[] args) { Car car = new Car(); car.setFuel(); car.run(); Truck truck = new Truck(); truck.setFuel(); truck.run(); } } interface Vehicle { public void setFuel(); public void run(); } class Car implements Vehicle { public void setFuel() { System.out.println("Car: Tank is full."); } public void run() { System.out.println("Car: Running."); } } abstract class MotorVehicle { public void setFuel() { System.out.println("MotorVehicle: Tank is full."); } abstract public void run(); } class Truck extends MotorVehicle { public void run() { System.out.println("Truck: Running."); } }
Output
Car: Tank is full. Car: Running. MotorVehicle: Tank is full. Truck: Running.
- Related Articles
- Difference Between Interface and Abstract Class in Java & C#
- Difference between abstract class and interface
- Difference between Abstract Class and Interface in C#
- Differences between abstract class and interface in Java
- Difference between Abstract Class and Interface in C# Program
- Difference Between Class and Interface in Java
- What is the difference between an interface and an abstract class in C#?
- Difference Between Thread Class and Runnable Interface in Java
- What is the difference between abstract class and a concrete class in Java?
- Differences between abstract class and concrete class in Java
- Differences between Interface and class in Java
- When to use an abstract class and when to use an interface in Java?
- Abstract class in Java
- Difference between Runnable and Callable interface in java
- Difference Between Iterator and Enumeration Interface in Java

Advertisements