Last Updated: 28 May, 2022
In Java, An interface is an abstract type that is used to specify a behavior that classes must implement. An Interface contains only method declaration(no method body) and constant variables without Constructor and static methods.
Use the interface keyword we can create Interface in Java but we cannot create objects of Interface.
Syntax of Java Interface
There are some couple of significant reasons for using Interface in Java:
Example 1 : A sample Java program of Inheritance
Output
Cow eats grass
Cow runs slow
Cow has total legs: 4
Example 2 : A sample Java program of Interface
Output
showCallHistory() method called.
showMissedCalls() method called.
showIncomingCalls() method called.
showOutgoingCall() method called.
public
.public
and abstract
.public
, static
and final
.private
, protected
, static
, final
access modifier with the interface, we can only use public
and abstract
access modifier. If not mention then it will be default.That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between Interface and Class in Java?
Interface and Class in Java are user define data types both looks similar to each other but there are many major differences let's see and learn.
Interface | Class |
---|---|
To create Interface we use interface keyword.
Syntax interface interface_name {}
|
To create class we use class keyword.
Syntax class class_name {}
|
We cannot create objects of Interface. | We can create objects of class. |
Interface does not contain Constructor | Class contains Constructors |
Interface contains only method declaration not definition. | Class contains complete method definition. |
Interface does not need access modifiers. | Class needs access modifiers. |
Interface does not contains static methods. | Class can contain static methods. |
Using Interface we can achieve Multiple Inheritance. | A class can extends only one class so we cannot achieve Multiple Inheritance. |
Interface is slower in speed. | Class is faster in speed. |
An Interface can never implements a Class | A class can implements any number of Interfaces. |
What is the difference between Abstract Class and Interface in Java?
There are following differences between Abstract Class and Interface, let's see the given table
Abstract Class | Interface |
---|---|
abstract keyword is used to declare abstract class. | interface keyword is used to declare interface. |
Abstract can have abstract methods and non-abstract methods both. | Interface can have only abstract method. |
Abstract Class does not suppport Multiple Inheritance. | Interface supports Multiple Inheritance. |
Abstract class can have final, non-final, static and non-static variable. | Interface can have only final and static variables. |
Abstract class can have Constructor and static method. | Interface can not have Constructor and static method. |
Abstract class can provide the implementation of an Interface. | Interface cannot provide the implementation of Abstract class. |