As we all know, abstraction refers to hiding the internal implementation of a feature and only giving the users the functionality. i.e. how it works (showing), what it does (showing) (hiding). Because an abstraction is achieved using both abstract classes and interfaces, Interface and Abstract classes are needed prerequisites. Let’s see the differences between them.
Properties | Interface | Abstract Class |
Type of methods: | An interface can have only abstract methods. | An abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also. |
Final Variables: | Variables declared in a Java interface are by default final. | An abstract class may contain non-final variables. |
Type of variables: | The interface has only static and final variables. | An abstract class can have final, non-final, static and non-static variables. |
Implementation: | Interface can’t provide the implementation of an abstract class. | An abstract class can provide the implementation of the interface. |
Inheritance vs Abstraction: | A Java interface can be implemented using the keyword “implements” | An abstract class can be extended using the keyword “extends”. |
Multiple implementations: | An interface can extend to another Java interface only. | An abstract class can extend another Java class and implement multiple Java interfaces. |
Accessibility of Data Members: | Members of a Java interface are public by default. | A Java abstract class can have class members like private, protected, etc. |
Note: also read about the Interface in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…