Interface in Java

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

What is an Interface in Java?

An interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. ... Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class. For example, we might have defined the following interface for classes that have a position.

public interface Locatable {
     public double getX();
     public double getY();
 }
 

With the interface defined, we will now need classes that claim to implement the interface. For this, we need to say so up front using an implements clause.

public class Ball implements Locatable { //...
 public class Paddle extends Block implements Locatable { //...
 

Any class that has such an implements clause has to define all of the methods defined in the interface. Otherwise, there's a compiler error. Thus, both Ball and Paddle must have getX and getY methods; since the interface says these methods take no parameters and return a double, they need to be defined this way in Ball and Paddle too. What makes interfaces useful is that they are types, too. The following would be completely legitimate.

Locatable loc = new Ball();
 System.out.println(loc.getX() + ", " + loc.getY());
 loc = new Paddle();
 System.out.println(loc.getX() + ", " + loc.getY());
 

You cannot create an instance of an interface: new Locatable() would be illegal. In some sense, with interfaces a class have two superclasses. Only one of these can be a true Java class, but the type conversion behavior makes it feel as if both the actual parent class and the implemented interface are superclasses.

Rules of Using Interface

  • Methods inside Interface must not be static, final, native or strictfp.
  • All variables declared inside interface are implicitly public static final variables(constants).
  • All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
  • Interface can extend one or more other interface.
  • Interface cannot implement a class.
  • Interface can be nested inside another interface.

 

What is JNI?

 

In computing, the Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.

What is a computer interface?

In computing, an interface is a shared boundary across which two separate components of a computer system exchange information. The exchange can be between software, computer hardware, peripheral devices, humans and combinations of these.

What is a software interface?

user interface - the keyboard, mouse, menus of a computer system. The user interface allows the user to communicate with the operating system. Also see GUI. software interface - the languages and codes that the applications use to communicate with each other and with the hardware.

What is the meaning of native code in Java?

Native code is computer programming (code) that is compiled to run with a particular processor (such as an Intel x86-class processor) and its set of instructions. If the same program is run on a computer with a different processor, software can be provided so that the computer emulates the original processor.

Interface implementation

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.

Example of Interface implementation

interface Moveable 
{
 int AVG-SPEED = 40;
 void move();
}

class Vehicle implements Moveable 
{
 public void move()
 {
  System .out. print in ("Average speed is"+AVG-SPEED");
 }
 public static void main (String[] arg)
 {
  Vehicle vc = new Vehicle();
  vc.move();
 }
}

Difference between an interface and an abstract class?

Abstract class

Interface

Abstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes. Interface is a Java Object containing method declaration but no implementation. The classes which implement the Interfaces must provide the method definition for all the methods.
Abstract class is a Class prefix with an abstract keyword followed by Class definition. Interface is a pure abstract class which starts with interface keyword.
Abstract class can also contain concrete methods. Whereas, Interface contains all abstract methods and final variable declarations.
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.
Abstract class does not support multiple inheritance Interface support multiple inheritance
Only complete Member of abstract class can be static Member of interface cannot be static

For Indepth understanding of Java Click on

Java Virtual Machines

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox