What is the difference between Java's abstract class and interfaces? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between Java's abstract class and interfaces?

19th Jun 2017, 12:29 PM
Haider Ali
Haider Ali - avatar
5 Answers
+ 1
An interface is a fully abstract class. If you're implementing an interface, you have to override all methods in there. Like an abstract class, it cannot be instantiated. Often you could use an abstract class instead of an interface, but if you want to use an abstract class, you must inherit from it. Because you can implement as much interfaces as you want, it's better to use them instead of inherit from an abstract class. I hope I could help😊
19th Jun 2017, 7:23 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
If you inherit from a class, this class is the superclass of your class which is the subclass. A subclass takes all methods of the superclass and uses them. Then, you can override some methods, for example you want to give your class a specific behaviour. If your superclass is the class names "animals", you could inherit from your brand new class named "elefant". An elefant is an animal, so it eats, sleep and so on. Bit there are differences too, an elefant has a special "voice". If you have a method "speak" you want to override it to give your elefant a voice. That's a good example of abstract methods:You do not know which voice your animal will have when your writing the "animal class", so you make your method abstract. If you aren't looking forwards to write your project with other programmers, you could write the "voice" method anyway without the abstract keyword and override it, but the possibility of bugs is bigger, because you could forget it. That's all an example.😊 You can only inherit from one class while you're able to implement as many interfaces as you want. If that specific interface fits perfectly into your uses, it's better to implement it instead of "wasting" the place for another superclass.
20th Jun 2017, 4:36 AM
Jonas Schröter
Jonas Schröter - avatar
0
nicely explained. What is the difference between inheritance and implementation? They seem to be similar phenomena but use difference keywords as 'extends' and 'implements'
19th Jun 2017, 8:43 PM
Haider Ali
Haider Ali - avatar
0
Understood. Thank you
20th Jun 2017, 5:31 AM
Haider Ali
Haider Ali - avatar
0
Glad I could help
20th Jun 2017, 11:35 AM
Jonas Schröter
Jonas Schröter - avatar