OOPS in JAVA 20
Interface in Java
An interface in java is a blueprint of a class. It has static constants and abstract methods only.
Java Interface also represents IS-A relationship.
Syntax
interface interface_name {
}
Example:
/*Actual Code*/ interface Moveable { int AVERAGE-SPEED=0; //what you declare void move(); } /*Compiler Code*/ interface Moveable { public static final int AVERAGE-SPEED=40; //what compiler sees public abstract void move(); }
NOTE : Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
Interface differs from a class in following ways:
1. You cannot instantiate an interface.
2. An interface does not contain any constructors.
3. All of the methods in an interface are abstract.
4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
5. An interface is not extended by a class; it is implemented by a class.
6. An interface can extend multiple interfaces.
NOTE : A class extends another class, an interface extends another interface but a class implements an interface.
Why we use Java interface??
1. It is used to achieve complete abstraction.
2. By interface, we can support the functionality of multiple inheritance.
3. It can be used to achieve loose coupling.
Rules for using Interface:
1. Methods inside Interface must not be static, final, native or strictfp.
2. All variables declared inside interface are implicitly public static final variables(constants).
3. All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
4. Interface can extend one or more other interface.
5. Interface cannot implement a class.
6. Interface can be nested inside another interface.
Example of Interface implementation :
interface Moveable { int AVG-SPEED = 60; void move(); } class Vehicle implements Moveable { public void move() { System.out.println("Average speed is"+AVG-SPEED"); } public static void main (String[] arg) { Vehicle vc = new Vehicle(); vc.move(); } } Output : Average speed is 60.
Multiple inheritance in Java by interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
Example:
interface Printable{ void print(); } interface Showable{ void show(); } class A implements Printable,Showable{ public void print(){ System.out.println("Hello"); } public void show(){ System.out.println("Java"); } public static void main(String args[]){ A a = new A(); a.print(); a.show(); } } Output: Hello Java
Interface inheritance:
A class implements interface but one interface extends another interface.
Example:
interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class Testinterface2 implements Showable{ public void print(){ System.out.println("Hello"); } public void show(){ System.out.println("Java"); } public static void main(String args[]){ Testinterface2 obj = new Testinterface2(); obj.print(); obj.show(); } } Output: Hello Java
Nested Interface in Java:
Note: An interface can have another interface i.e. known as nested interface.
Example:
interface printable{ void print(); interface MessagePrintable{ void msg(); } }
Difference between abstract class and interface?
Abstract class | Interface |
Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. |
Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
Abstract class can have static methods, main method and constructor. | Interface can't have static methods, main method or constructor. |
Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
Example: public abstract class Shape { public abstract void draw(); } | Example: public interface Drawable{ void draw(); } |
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )