Categories: Java

Interface vs Abstract class

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.

Interface vs Abstract class
PropertiesInterfaceAbstract 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

Follow Me

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago