Java Interface

20
JAVA INTERFACES

description

this document explains Java interfaces - A very important concept in Java.

Transcript of Java Interface

Page 1: Java Interface

JAVA

INTERFACES

Page 2: Java Interface

Inheritance

•OOP allows you to derive new classes from existing classes. This is called inheritance.

• Inheritance is an important and powerful concept in Java. Every class you define in Java is inherited from an existing class.

• Sometimes it is necessary to derive a subclass from several classes. In Java only one parent class is allowed.

•With interfaces, you can obtain effect of multiple inheritance.

Page 3: Java Interface

What is an Interface?

•An interface is a class like construct that contains only constants and abstract methods.

•Cannot be instantiated. Only classes that implements interfaces can be instantiated. However, final public static variables can be defined to interface types.

•Why not just use abstract classes? Java does not permit multiple inheritance from classes, but permits implementation of multiple interfaces.

Page 4: Java Interface

What is an Interface?

•Protocol for classes that completely separates specification/ behavior from implementation.

•One class can implement many interfaces

•One interface can be implemented by many classes

•By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism.

Page 5: Java Interface

Why an Interface?

•Normally, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible.

• In a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses.

• Interfaces are designed to support dynamic method resolution at run time.

Page 6: Java Interface

Why an Interface?

• Interfaces disconnect the definition of a method or set of methods from the inheritance hierarchy.

•Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface.

Page 7: Java Interface

Defining Interfaces

• Interface is just like any other class but with a major difference. It contains methods and variables like a class but the difference is that interfaces define only abstract methods and final fields.

•This means that the methods in an interface does not specify any code and the data fields contain only constants.

•So the responsibility to define the code is with the class that implements the interface

Page 8: Java Interface

Syntax for defining an interface

interface Interfacename{

variable declaration;methods declaration;

}

Here, Interface is the keyword and InterfaceName is any Java variable(just like class name).

Page 9: Java Interface

Variables are declared as follows:

static final type VariableName = Value;

•Variables are declared as constants.

•Methods declaration will contain only a list of methods without any body statements. Ex: return-type methodName1 (parameter_list);

Page 10: Java Interface

A small example of an interface definition

interface Item{

static final int code = 1001;static final String name = "FAN";void display();

}

Page 11: Java Interface

•Interfaces can also be extended/sub-interfaced from other interfaces using the keyword extends.

Page 12: Java Interface

Extending Interfaces

Page 13: Java Interface

•All variables in an interface are treated as constants even thought the keywords final and static are not present.

•Combining several interfaces together into a single interface can be done separating the super interfaces with commas

•An interface cannot extend classes.

Page 14: Java Interface
Page 15: Java Interface

Interfaces vs Abstract Classes

• In an interface, the data must be constants.• An abstract class can have all types of data.

• Each method in an interface has only a signature without implementation.

• An abstract class can have concrete methods. An abstract class must contain at least one abstract method or inherit from another abstract method.

Page 16: Java Interface

Interfaces vs Abstract Classes.

Since all the methods defined in an interface are abstract methods, Java does not require you to put the abstract modifier in the methods in an interface, but you must put the abstract modifier before an abstract method in an abstract class.

Page 17: Java Interface

Interfaces & Classes

Object Class1

Interface1Interface1_1

Interface1_2

Class2

Interface2_1

Interface2_2

• One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. • When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.

Page 18: Java Interface

Packages

• Packages are the solution to group classes and interfaces together.

• Benefit : Searchability, Systematic code Reusability and Maintainability

• package package_name{ CODE HERE;}

Import package_name;

Page 19: Java Interface

JAVA API PACKAGES

• Java provides many built-in classes. They are categorized based on their functionality.

• import java.package_name.class_name

Page 20: Java Interface

THANK YOU!!!