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:
would be illegal. In some sense, with interfaces a class have two new Locatable()
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.
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.
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.
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(); } }
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 |
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
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.