what is the different between abstract and interface | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is the different between abstract and interface

17th Sep 2016, 6:19 AM
Aliyu Mohammed Yelwa
4 Answers
+ 4
An abstract class has at least one abstract method (a method that is declared but not defined). An interface is a class with only abstract methods. In both cases, you can't instanciate objects of the class, you have to make a class that inherit from it, with a proper implementation of the abstract methods.
17th Sep 2016, 9:10 AM
Zen
Zen - avatar
+ 2
1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior. 2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables. 3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc.. 4.Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”. 5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces. 6.A Java class can implement multiple interfaces but it can extend only one abstract class. 7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists. 8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection
17th Sep 2016, 1:30 PM
asghar beikmohammadi
asghar beikmohammadi - avatar
+ 1
Interfaces allow us to describe a set of behaviours which a component must provide functionality to when they implement that particular interface. Also you can think of interfaces as contracts/promise between you and the compiler that says something along the line of " if any class implements my interface, they must override every single behaviour within the interface". On the other hand, when you delcare a class as abstract that gives you the ability to also declare methods as abstract which means any class that inherits that particular abstract class must override all those abstract behaviours and provide functionality to them. Finally this is known as ABSTRACTION (ensuring that implementation details are not visible to the user, rather only the functionality. So a user should know what that thing does rather than how it does it). For more details be sure to visit the java reference page.
17th Sep 2016, 8:22 AM
Ousmane Diaw
0
interface means 100% abstraction while abstract class or method could achieve 0-100% abstraction.
19th Sep 2016, 12:49 AM
Ankur Sharma
Ankur Sharma - avatar