Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sampath Kumar S Assistant Professor, SECE

Similar presentations


Presentation on theme: "Sampath Kumar S Assistant Professor, SECE"— Presentation transcript:

1 Sampath Kumar S Assistant Professor, SECE
Interfaces Sampath Kumar S Assistant Professor, SECE

2 12/28/2018 Interfaces An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. 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. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. Sampath Kumar S, AP

3 12/28/2018 Why use Interface? There are mainly three reasons to use interface. They are given below. It is used to achieve fully abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling. Sampath Kumar S, AP

4 Similarities Between Class and Interface:
12/28/2018 Similarities Between Class and 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, and their corresponding bytecode file must be in a directory structure that matches the package name. Sampath Kumar S, AP

5 Difference between Interface and Class
12/28/2018 Difference between Interface and Class An interface is different from a class in several ways, including: You cannot create instance(Create Object) to an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. 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. Sampath Kumar S, AP

6 12/28/2018 Cont.., Sampath Kumar S, AP

7 Defining an Interfaces:
12/28/2018 The interface keyword is used to declare an interface. Access_Specifier interface <name> { Return_type <method_name_1> (parameter_list); type final <variable_name_1>; Return_type <method_name_2> (parameter_list); type final <variable_name_2>; // … Return_type <method_name_N> (parameter_list); type final <variable_name_N>; } Note: The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members. Sampath Kumar S, AP

8 Example : Defining an Interfaces:
12/28/2018 Example : Defining an Interfaces: Sampath Kumar S, AP

9 12/28/2018 Example: Interface interface IT{ void itClass(); } class thirdIt implements IT{ public void itClass(){ System.out.println(“This is 3rd IT"); public static void main(String args[]){ thirdIt obj = new thirdIt(); obj.itClass(); } Output: This is 3rd IT Sampath Kumar S, AP

10 Multiple inheritance in Java by interface
12/28/2018 Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance Sampath Kumar S, AP

11 Example: Multiple Inheritance
12/28/2018 Example: Multiple Inheritance 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("Welcome"); } Sampath Kumar S, AP

12 Example: Cont.., Output: Hello Welcome
12/28/2018 Example: Cont.., public static void main(String args[]){ A obj = new A(); obj.print(); obj.show(); } } Output: Hello Welcome Sampath Kumar S, AP

13 How Multiple Inheritance possible?
12/28/2018 How Multiple Inheritance possible? interface Printable{ void print(); } interface Showable{ } class A implements Printable,Showable { public void print(){ System.out.println("Hello"); } public static void main(String args[]){ A obj = new A(); obj.print(); } Output: Hello Sampath Kumar S, AP

14 Example: Extends & Implements
12/28/2018 Example: Extends & Implements interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class A implements Showable{ public void print(){ System.out.println("Hello"); public void show(){ System.out.println("Welcome"); Sampath Kumar S, AP

15 Example: Cont.., Output: Hello Welcome
12/28/2018 Example: Cont.., public static void main(String args[]){ A obj = new A(); obj.print(); obj.show(); } } Output: Hello Welcome Sampath Kumar S, AP

16 Marker or Tagged interface
12/28/2018 Marker or Tagged interface An interface that have no member is known as marker or tagged interface. They are used to provide some essential information to the JVM so that JVM may perform some useful operation Example:     public interface Serializable{   }   Sampath Kumar S, AP

17 12/28/2018 Sampath Kumar S, AP

18 12/28/2018 Thank You  Sampath Kumar S, AP


Download ppt "Sampath Kumar S Assistant Professor, SECE"

Similar presentations


Ads by Google