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

what is the difference between abstract class and interface class?

what is use of those classes?

30th Nov 2022, 3:33 PM
mon
6 Answers
+ 5
mon You can implement other properties by overriding but you dont have to. Abstract propeties are must or an error will occur
1st Dec 2022, 9:33 AM
Toni Isotalo
Toni Isotalo - avatar
+ 2
Abstract class can only be inherited by other classes. You can not create an instances of it. Abstract class can also contain abstract propeties which the child class must implement when inheriting the class. Interface is a class which other classes can implement. It describes the structure of the implementing classes. It contains properties that the implementing class must provide
30th Nov 2022, 6:29 PM
Toni Isotalo
Toni Isotalo - avatar
1st Dec 2022, 6:36 AM
Tibor Santa
Tibor Santa - avatar
+ 2
You can review this article for a more detailed comparison: https://www.javatpoint.com/difference-between-abstract-class-and-interface One important element of OOP concepts, that supports code reuse, is "dynamic polymorphism". In Java this is achieved by inheritance. When two different objects share some common features, it can make sense for them to have a common ancestor in the class hierarchy. So the common features only need to be implemented in one location. It is true for both abstract classes and for interfaces, that they can have method implementations too (in case of interface, this is called default method). Only abstract classes can have internal fields though. interfaces can only have constants (declared as public static final). Another important difference is that a class can extend only a single parent class (whether it is a normal or abstract class) but it can implement multiple interfaces. Typically we would define an interface to express a certain behavior, which is applicable for a class. Examples are Serializable, Comparable, or Runnable interfaces. Other times we express a contract that the subclasses must follow, in terms of functionality, for example the classes ArrayList and LinkedList both implement the List interface.
1st Dec 2022, 10:07 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Toni_Isotalo Is we must implement only abstract properties in abstract class and other properties we don't??
1st Dec 2022, 1:24 AM
mon
- 1
abstract class contains an abstract keyword on the declaration. Interface is a sketch that is used to implement a class.
30th Nov 2022, 4:00 PM
Anonymous
Anonymous - avatar