Introduction To Java Interface
Java is a widely popular and robust programming language. It is an object-oriented programming language. Java is easy to learn for students as it provides a modular structure for programming. To achieve good command over the Java language, students need to understand the basic concepts of object-oriented programming such as object, class, polymorphism, abstraction, inheritance, etc. This article helps to understand the interface concept in Java.
The interface is similar to the class. It is a set of methods and variables. A class implements the interface to inherit the methods and variables. It is possible to implements multiple interfaces. Hence, users can achieve abstraction and multiple inheritances by using the interface. The writing interface is almost similar to the class. All methods of the interface need to define in the class.
How Interface Is Different From Class?
- The main difference is that you cannot create an instance for the interface.
- It is implemented by the class not extended by the class.
- Methods declared in the interface by default they are abstract.
- It does not contain a constructor.
- An interface can extend multiple interfaces also can extend from one more interface.
- An interface can declare in another interface called a nested interface.
- Interface stores as a .java file.
Examples To Implement Java Interface
Given below are the examples of Java Interface:
Example #1: Declaring Interface
By default, interface and methods in an interface are abstract there is no need to use the ‘abstract’ keyword. You can declare an interface by using the interface word. The variables of the interfaces must be initialized at the time of interface declaration. The class has to define the methods declared in the interface.
Code:
public interface Earth
{
public void human ();
}
Example #2: Implementing Interface by the Class
The class can implement an interface by using implements keyword. And can implements any number of interfaces. The class needs to override all methods declared in the interface.
Code:
interface Planet
{
public void Earth ();
}
class Galaxy implements Planet
{
public void Earth ()
{
System.out.println("Earth is a planet");
}
public static void main (String args[])
{
Planet g = new Galaxy ();
g.Earth();
}
};
Output:
Example #3: Extending Interface
An interface can extend another interface by using extends keyword. An interface can extend many interfaces.
Code:
public interface Earth
{
public void AB();
}
public interface Jupiter extends Earth
{
public void DC();
}
class Planet implements Jupiter
{
public void AB()
{
System.out.println (“I am Earth”);
}
public void DC()
{
System.out.println(“I am Jupiter”);
}
public static void main (String[] args)
{
Planet p= new planet();
p.Earth();
p.Jupiter();
}
}
Output:
Advantages & Disadvantages Of Java interface
Given below are the advantages & disadvantages mentioned:
Advantages:
- It achieves full abstraction.
- It avoids repetition of code and allows to reuse of the code.
- Provides modular structure to the program.
- Using the interface can break up dependencies and achieve loose coupling.
Disadvantages:
- Java interface saves signature of methods only not the definition.
- Interface makes slow execution of the program.
Important Points Of Interface
Given below are important points of interface:
- Through interface can provide the specification for method prototype.
- Java does not support multiple inheritances hence using the interface can achieve multiple inheritances.
- An Interface can point to an object of classes.
- An Interface cannot be instantiated.
- Variables declared in the interface by default are static, final, and public.
Conclusion
Java is an object-oriented programming language. All the concepts of object-oriented programming are interesting to learn. The interface is also one of the most important concepts in the Java language. The interface is the default abstract class and having only abstract methods. The interface contains the behavior of the class. It is a reference type in java.
Recommended Articles
This is a guide to What is a Java Interface? Here we discuss a brief overview on java interface, its concept, advantages & disadvantages along with examples. You may also have a look at the following articles to learn more–