Interface in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history
The interface is a pure abstract class. They are syntactically similar to classes, but you cannot create an instance of an Interface and their methods are declared without anybody. The interface is used to achieve complete abstraction in Java. When you create an interface it defines what a class can do without saying anything about how the class will do it.
Syntax :
interface interface_name { }
Example of Interface
interface Moveable { int AVERAGE-SPEED=40; void move(); }
NOTE: Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
Rules for 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 the public or abstract keyword.
- The interface can extend one or more another interface.
- An interface cannot implement a class.
- An interface can be nested inside another interface.
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(); } }
Output :
Average speed is 40.
Interfaces support Multiple Inheritance
Though classes in Java don't support multiple inheritances, but a class can implement more than one interface.
interface Moveable { boolean isMoveable(); } interface Rollable { boolean isRollable } class Tyre implements Moveable, Rollable { int width; boolean isMoveable() { return true; } boolean isRollable() { return true; } public static void main(String args[]) { Tyre tr=new Tyre(); System.out.println(tr.isMoveable()); System.out.println(tr.isRollable()); } }
Output :
true true
Interface extends other Interface
Classes implement interfaces, but an interface extends another interface.
interface NewsPaper { news(); } interface Magazine extends NewsPaper { colorful(); }
Difference between an interface and an abstract class?
Abstract class | Interface |
---|---|
Abstract class is a class which contains one or more abstract methods, which has to be implemented by its subclasses. | An 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. |
An abstract class is a Class prefix with an abstract keyword followed by a Class definition. | An interface is a pure abstract class which starts with interface keyword. |
An 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. |
Thanks for sharing this information. This is really useful. Keep doing more.
ReplyDeleteIELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai