New course! Every coder should learn Generative AI!
Try a free lesson+ 9
Source: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
+ 7
They are useful when you want to have two classes use the same methods in their own way.
They are also useful for abstraction and if you want them to easily interact with eachother outside their class implementations.
Simplest example of this is having an object array that can hold different objects, but its valid/objects are compatible since they implement the same interface; but that also works with extends...
It works as multiple inheritance only because interfaces can extend other interfaces and classes cant extend more than 1 class.
In reality, I have only seen interfaces being used to teach (school setting and assignments) and when using APIs.
Event listeners heavily use interfaces.
+ 7
I prefer just using classes for everything; never used interfaces for any personal projects
+ 4
Interface (abstraction) is the way how to force other programmers to write their inherit methods with common specific features and names of methods. Advantage of it is a standard expected way how to use some types of methods - because it has common names in different classes.
An Interface is necessary as syntax if you want to define a function with a lambda expression.
Interfaces are preferred in modern API in compare with an abstract class.
+ 4
Anirudh Sharma , If I see in partial code normal class with empty methods, might I delete it because as I can see it does nothing, but if it is abstract class I understand what author are doing here. And if it is abstract you must implement methods, but with normal empty methods not.
+ 3
Anirudh Sharma , abstraction has a sense if you want to define parent class but not implement methods directly, because you let others do it in their own way. Then you can write methods with empty bodies, but using abstract class or Interfaces instead is more understandable and readable.
+ 3
Another reason to use interfaces is Liskov's substitution principle (the L in SOLID) which is a key design element in Java's polimorphism.
https://en.m.wikipedia.org/wiki/Liskov_substitution_principle
An example, when you define a list you would write something like:
List<Integer> list = new ArrayList<>();
Later you decide that a LinkedList would have been a better choice (e.g. performance reasons) - then this is the only line you have to change because both data types have the same methods, as they implement the List interface. The rest of your program will still work.
Same story with streams.
+ 3
Mirielle🐶 [Inactive] After reading every response posted so far, I'm surprised by how most people focus so much on the mechanics of an interface or some academic explanation or focused on irrelevant comparisons to abstract classes or multiple inheritance.
I mean no disrespect to those making an attempt to help. I believe the challenge is due to a lot of bad information and examples in online articles and potentially, a lack of practical exposure at the university level. It's not entirely clear to me.
When I have a moment over the next 24 hours, I'll try to post an explanation purely from the practical understanding and various real world usages in professional software engineering.
In the meantime, the answers that stand out from the rest with relevant information are from ₦ØØⱤ ₳Ⱡł and Tibor Santa.
+ 2
if class has "implements Runnable", there must be implemented run() method
public class Runnable_test implements Runnable {
public static void main(String[] args) {
new Thread(new Runnable_test() ).start();
}
public void run() {
System.out.println("Hello" );
}
}
+ 2
₦ØØⱤ ₳Ⱡł - "final variables" are in fact constants
"In addition, an interface can contain constant declarations. All constant values defined in an interface are implicitly public, static, and final. Once again, you can omit these modifiers."
https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html
+ 1
Unfortunately I need to go to bed. Maybe zemiak knows more about Runnable.
But here you find another interesting stuff: marker interfaces
https://www.baeldung.com/java-marker-interfaces
+ 1
This is not an answer.
Denise Roßberg, in one of my previous projects, i just created one, base, normal class, and extended from it many other, related classes.
Certain methods which i wanted to be common to all, or at least have a default were store in the base, then reused or overridden as required.
Was it a wrong way? Should i have had used abstract classes? What difference it made?
+ 1
Thanks zemiak
+ 1
Multiple inheritance of methods/behaviour rather than attributes, I think is what interfaces can achieve.
0
zemiak, "more understandable and readable", that what my class name can do too