Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface.

Similar presentations


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

1 Interface

2 Interface An interface is a collection of abstract methods.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements

3 Interface An interface is similar to a class in the following ways:
An interface can contain any number of methods. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. The bytecode of an interface appears in a .class file. Interfaces appear in packages

4 Interface Interface is different from a class in several ways, including: An interface does not contain any constructors. All of the methods in an interface are abstract. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

5 Declaring Interfaces The interface keyword is used to declare an interface. interface interfacename { abstract methods; } An interface is implicitly abstract. Do not need to use the abstract keyword when declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public.

6 Declaring Interfaces interface in { void show(); }

7 Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration

8 Implementing Interfaces
class check implements in { public void show(){System.out.println("show in interface");} public static void main(String[] args) System.out.println("Hello World!"); check obj=new check(); obj.show(); } When you implement an interface method, it must be declared as public

9 Implementing Interfaces
The signature of the interface method and the same return type should be maintained when overriding the methods. An implementation class itself can be abstract and if so interface methods need not be implemented. A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, similarly to the way that a class can extend another class.

10 Extending Interfaces:
An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

11 Extending Interfaces:
Store as in1.java interface in1 { void show1(); } Store as in2.java interface in2 extends in1 void show2();

12 Extending Interfaces:
Class check implements in2 { public void show1(){System.out.println("show1");} public void show2(){System.out.println("show2");} public static void main(String[] args) System.out.println("Hello World!"); in_check obj=new in_check(); obj.show1(); obj.show2(); }

13 Extending Multiple Interfaces:
A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface. The implements keyword is used once, and the parent interfaces are declared in a comma-separated list. class in_check implements in1,in2 Where in1 and in2 are interfaces

14 Variables in Interfaces
Interfaces can use to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values. When you include that interface in a class all of those variable names will be in scope as constants.

15 Variables in Interfaces
interface in1 { int a=10; void show1(); } class in_check implements in1,in2 { public void show1(){System.out.println("show1"+a);} public void show2(){System.out.println("show2"+a);} public static void main(String[] args) in_check obj=new in_check(); obj.show1(); obj.show2(); Cannot modify a -> it is constant

16 Variables in Interfaces


Download ppt "Interface."

Similar presentations


Ads by Google