Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface.

Similar presentations


Presentation on theme: "Interface."— Presentation transcript:

1 Interface

2 Interface In Java, an interface is a reference type, similar to a class Can contain constants and method signatures There are no method bodies Cannot be instantiated - they can only be implemented by classes or extended by other interfaces Variables and the methods declared in interfaces are public by default An interface can extend another interface A class can implement multiple interfaces

3 Interface A class that contains only abstract methods and/or named constants How Java implements multiple inheritance To be able to handle a variety of events, Java allows a class to implement more than one interface

4 Interface Why Interface?
To reveal an object's programming interface (functionality of the object) without revealing its implementation To have unrelated classes Implement similar methods (behaviors) To model multiple inheritance interface Int1 { ... } interface Int2 { class Test implements Int1, Int2 { class Test1{ ... } interface Int3 { class Test extends Test1 implements Int3{

5 Interfaces and Abstract classes can not be instantiated
Example of an interface declaration: public interface Interface1 { public void set(int i); public int get(); } Interface1 i1 = new Interface1 (); Interfaces and Abstract classes can not be instantiated

6 Interface Example interface Shape { private double pi=3.14;
public void getArea(int a); } class Circle implements Shape { public void getArea(int r) { double area=pi*r*r; System.out.println("Area is " + area); public class interfaceDemo { public static void main (string args[]) { Circle ci=new Circle(); c1.getArea(3);

7 Interface Example interface shape{ void area(); }
}      class A6 implements shape{   public void area(){System.out.println(“This is area");}   public static void main(String args[]){   A6 obj = new A6();   Obj.area();     }  


Download ppt "Interface."

Similar presentations


Ads by Google