Technical

Difference Between Abstract Class and Interface

Difference Betan Abstract Class and Interface


Abstract class and Interface are a core part of the JAVA programming language. Both Abstract class and Interface, primarily used for abstraction. Abstraction: Hiding the internal implementation of the feature and only showing the functionality to the users. Both are very different cannot be used interchangeably.

difference Between Abstract Class and Interface

To understand the difference between abstract class and interface, we need to know what abstract class is and what is an interface and how they work.

 

Difference between abstract class and interface

ABSTRACT CLASS INTERFACE
It may contain non-final variables. Variables are declared in an interface are by default final.
An abstract class can be extended using the keyword “extends”. The interface should be implemented using the keyword “implements”.
An abstract class can have a public, protected, private, and default modifier. Interface methods are by default public. You cannot use any other access modifier with it.
It is faster than the interface. An interface is somewhat slower and requires extra indirection.
It can extend only one abstract class. An interface can implement multiple interfaces.
An abstract class can have constructors. An interface can extend another Java interface.
It can have default method implementation Interfaces provide pure abstraction and cannot have implementation at all

 

ABSTRACT CLASS


In Java programming language, abstraction is a process where you show only “relevant” data and “Hide” unnecessary details of an object from the user to increase efficiency by reducing complexity. A class that is declared using the “abstract” keyword is known as abstract class. An abstract class cannot be instantiated, which means you are not allowed to create an object of it. The abstract class allows concrete methods as well it does not provide 100% abstraction. It provides partial abstraction.

Syntax :-  abstract class A {

Abstract void mymethod();

Void anotherMethod() {

}  }

INTERFACE


As we know the abstract class is used to achieve partial abstraction. Unlike abstract class, an interface is used for full abstraction. The interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in an interface are by default abstract. The variables declared in an interface are public, static, and final by default.

Syntax :-

  interface MyInterface {

                        public void method1();

                        public void method1();

                        }

About the author

Mayuri Bhoyar

Leave a Comment