9.6 Differences between the abstract class and the interface

Hello everyone, as we have seen the abstract class and the interface, in this video we will see the difference between two. First we’ll start with the abstract class. The abstract keyword in Java is used to create or declare an abstract class A class can inherit the properties and methods of an abstract class using the extended keyword.. An abstract class may have abstract or non-exabstract methods defined in it. Abstract methods are those for which no implementation is provided. Java does not support multiple inheritances via classes. Abstract classes, like any other class, do not support multiple inheritances. Abstract class members or attributes can be private, protected or public. Now, the interface. In Java, the interface keyword is used to create or declare a new interface. To implement an Interface in Java, you can use the implements keyword. An interface can only contain abstract methods. We can only provide the definition of the method but not its implementation. Support for multiple inheritances in Java is provided through interfaces. This is because interfaces provide a complete abstraction. The attributes or members of an Interface are always public. When to use abstract class and interface? Abstract classes can provide partial or complete abstraction. Interfaces, on the other hand, always provide a complete abstraction. An abstract parent class can be created for a few classes that have common features. Abstract classes are also preferred if you want more freedom of action. Interfaces are preferred when defining a basic structure. The programmer can then build anything with this structure. Interfaces also support multiple inheritances. Thus, a single class can implement several interfaces. Overall, it is a matter of choice and the task that must be accomplished. The abstract class and interface are suitable for different purposes and should be used accordingly. That’s all for this video, I hope you understood a little the difference between the abstract class and the interface. I’ll see you around.