what is interface in java

what is interface in java  

An interface is a reference type that contains only methods or constants. It cannot contain instance variables or constructors.

An interface is similar to a class, but with some restrictions on its usage. An interface can only be implemented by classes, and it’s not possible to create objects from an interface directly.

what is interface in java

The methods of an interface are referred as ‘operations’ because they don’t represent properties of an object like fields and methods in classes do.

The operations of an interface must be public and static. A Java interface can extend one or more interfaces as well as implement zero or more interfaces. Read on to know more about interfaces in Java.

Why to use Java Interface

Java interface allows a programmer to define a set of properties (methods and constants) that a class will implement. Interface provides a contract between a programmer and the classes that implement the interface

– making it easy to use and maintain code. Without interfaces, you would have to either duplicate code between your classes or make your classes responsible for more than one task

– making for an overly complex application. With interfaces, you can group methods and constants together and then require all your classes to implement these methods and constants.

This makes your code easier to maintain because it requires less code and there is no duplication of code across your application. When you have to change or update something in one place, it will be reflected in all other places as well.

Defining your own interfaces in Java

You can define your own Java interfaces using this syntax

– InterfaceName interfaceName = new InterfaceName(); // Where InterfaceName is the name of the interface. Here, InterfaceName is the name of the interface. Remember that interfaces can only contain constant methods, or methods that don’t have any implementation.

Let’s take an example of an interface named MyJvmPrograms. This is how it will look like – public interface MyJvmPrograms { // This is the first line of the interface. public void printHelloWorld(); // This is the second line of the interface.

public String sayHelloTo(String toWho); // This is the third line of the interface. public String getGreetingMessage(); // This is the fourth line of the interface. }

Here, you can see that we have three methods in our interface. All three methods are public and static. We can’t have any instance variables in an interface since it is a reference type.

Difference between Java Interface and Java Class

There are some key differences between a Java Interface and Java Class –

– An interface cannot have any implementation while a class can have its own implementation.

– An interface cannot have any instance variables while a class can have its own instance variables.

– An interface cannot have any constructor while a class can have its own constructor.

– An interface cannot be instantiated while a class can be instantiated.

– An interface cannot extend another class while a class can extend another class.

Defining methods in an interface

As we already know that methods in an interface are public, static, and are used to declare operations. Let’s take an example of an interface named MyJvmPrograms.

This is how it will look like – public interface MyJvmPrograms { // This is the first line of the interface. public void printHelloWorld(); // This is the second line of the interface. public String sayHelloTo(String toWho); // This is the third line of the interface.

public String getGreetingMessage(); // This is the fourth line of the interface. } Here, you can see that we have three methods in our interface.

All three methods are public and static. We can’t have any instance variables in an interface since it is a reference type.

Can we have a constructor in an interface?

Yes, you can have a constructor in an interface. You can also have constructor with arguments in an interface. Let’s take an example of an interface named MyJvmPrograms.

This is how it will look like – public interface MyJvmPrograms { // This is the first line of the interface. public MyJvmPrograms getInstance(String input); // This is the second line of the interface.

public void printHelloWorld(); // This is the third line of the interface. public String sayHelloTo(String toWho); // This is the fourth line of the interface.

public String getGreetingMessage(); // This is the fifth line of the interface. } Here, you can see that we have three methods in our interface. One of the methods has an instance variable.

Use of Interface – what is interface in java

For the following classes, you can implement the interface – – You can implement an interface in the following cases – – When you want all classes to implement the method defined in the interface.

– When you want to use the method defined in the interface. – When you want a class to extend another class and implement an interface.

Limitation on using Interface

While, it is true that Java interfaces can be used for many different scenarios, it is important to be aware of their limitations as well.

– Java interfaces can only be used for method signatures and constant values.

– There is no way to define field values in an interface.

– There is no way to define constructors in an interface.

– There is no way to extend another class in an interface.

– There is no way to inherit from another class in an interface.

– Interfaces cannot be implemented in a static context.

Conclusion – what is interface in java

It is important to note that Java interfaces are not the same as Java classes. Classes are used to code functionality whereas interfaces are used to declare functionality.

Classes can implement one or more interfaces while interfaces can only be implemented by classes. Once you understand these key differences, it becomes easier to see why interfaces can be used in so many situations.

In fact, it becomes easier to see why interfaces have become so popular in modern programming languages.

Add a Comment

Your email address will not be published. Required fields are marked *