Java 8 Interface vs Abstract Class
a thread...
a thread...
Before Java 8,
the skies were blue,
mountains were green,
the air was breezy..
Then came Java 8, with its new Interfaces.
Now, the interfaces can have implementations the same as Abstract classes and this removed the very fundamental diff between interface & abstract class.
the skies were blue,
mountains were green,
the air was breezy..
Then came Java 8, with its new Interfaces.
Now, the interfaces can have implementations the same as Abstract classes and this removed the very fundamental diff between interface & abstract class.
Lets try to answer the below questions:
1. What changes are introduced in the Java 8 Interface?
2. Why was the need to put implementations in Interface?
3. Is there really any diff between Interface and Abstract class now?
4. Which one to use now?
1. What changes are introduced in the Java 8 Interface?
2. Why was the need to put implementations in Interface?
3. Is there really any diff between Interface and Abstract class now?
4. Which one to use now?
1. Changes are introduced in the Java 8 Interface:
The interface can now have default methods with implementations.
The interface can now have static methods with implementations.
The interface can now have default methods with implementations.
The interface can now have static methods with implementations.
2. Why was the need to put implementations in Interface?
To support lambda functions better, the default method is introduced without breaking the existing implementations.
To provide backward compatibility to the Collection Framework with the new lambda functions.
To support lambda functions better, the default method is introduced without breaking the existing implementations.
To provide backward compatibility to the Collection Framework with the new lambda functions.
And, static methods are introduced so that we can add the utils method directly in the interface without having to create a new class.
It's helpful because prior to this if we had to create a utils method for interface we were forced to create a class.
It's helpful because prior to this if we had to create a utils method for interface we were forced to create a class.
That means Abstract classes can have implementations and now the interfaces can also have it.
So ain't they same now?
So ain't they same now?
3. Is there really any diff between Interface and Abstract class now?
Yes they're still different!
An abstract class can have a state, its methods can access a state of any of its instances.
But the interface can't have a state.
Let's understand this with an example.
Yes they're still different!
An abstract class can have a state, its methods can access a state of any of its instances.
But the interface can't have a state.
Let's understand this with an example.
Other Diff:
Abstract classes can have constructors.
Interfaces cannot have any constructor hence we cannot write any logic on object creation.
Abstract classes can have constructors.
Interfaces cannot have any constructor hence we cannot write any logic on object creation.
An abstract class can declare instance variables, with all possible access modifiers, and they can be accessed in child classes.
An interface can only have public, static, and final variables and can't have any instance variables.
An interface can only have public, static, and final variables and can't have any instance variables.
An abstract class can declare instances and static blocks, which is not possible with interfaces.
The interface can have a single abstract method that can refer to a lambda expression. But abstract class can't.
The interface can have a single abstract method that can refer to a lambda expression. But abstract class can't.
4. Which one to use now?
Of course, an Interface.
Because:
You can use multiple Interfaces in concrete classes and also extend one parent class.
Use Abstract class only when required because then you can't extend other classes so it's a bit restrictive.
Of course, an Interface.
Because:
You can use multiple Interfaces in concrete classes and also extend one parent class.
Use Abstract class only when required because then you can't extend other classes so it's a bit restrictive.
Do you like reading such an in-depth thread on Java?
Then you might also like my previous thread on Java:
Then you might also like my previous thread on Java:
Thanks for reading!
Hi, I'm Vikas!
I write a thread every Mon, Wed & Fri on
Java, Javascript & Fullstack Development.
To read all my future threads follow @vikasrajputin
Any Query, Feedback & Suggestions?
Put them in the comments below.
See you in the comments :)
Hi, I'm Vikas!
I write a thread every Mon, Wed & Fri on
Java, Javascript & Fullstack Development.
To read all my future threads follow @vikasrajputin
Any Query, Feedback & Suggestions?
Put them in the comments below.
See you in the comments :)
جاري تحميل الاقتراحات...