Interfaces
-
Upload
jai-marathe -
Category
Technology
-
view
380 -
download
3
description
Transcript of Interfaces
![Page 1: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/1.jpg)
Interfaces in JAVA
By Jai Vikas Marathe
![Page 2: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/2.jpg)
Agenda
What is Interface?
Why Interface?
Interface as a Type
Interface vs. Class
Defining an Interface
Implementing an Interface
Implementing multiple Interface's
Inheritance among Interface's
Rewriting an Interface
![Page 3: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/3.jpg)
What is an Interface?
![Page 4: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/4.jpg)
What is an Interface?
It defines a standard and public way of specifying the behaviour of classes
All methods of an interface are abstract methods
![Page 5: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/5.jpg)
Example: Interface
// Note that Interface contains just set of method
// signatures without any implementations.
// No need to say abstract modifier for each method
// since it assumed.
public interface Relation {
public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object b);
public boolean isEqual( Object a, Object b);
}
![Page 6: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/6.jpg)
Example 2: Interfacepublic interface OperateCar {
// constant declarations, if any
// method signatures
int turn(Direction direction, double radius, double startSpeed, double endSpeed);
int changeLanes(Direction direction, double startSpeed, double endSpeed);
int signalTurn(Direction direction, boolean signalOn);
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
//some more methods
![Page 7: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/7.jpg)
Why Interface?
![Page 8: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/8.jpg)
Why do we use Interfaces?Reason #1
To reveal an object's programming interface (functionality of the object) without revealing its implementation
![Page 9: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/9.jpg)
Why do we use Interfaces?Reason #2
To have unrelated classes implement similar methods (behaviours)
Example:
Class Line and class MyInteger
Interface:
checkIsGreater(Object x, Object y)
checkIsLess(Object x, Object y)
checkIsEqual(Object x, Object y)
![Page 10: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/10.jpg)
Why do we use Interfaces?Reason #3
To model multiple inheritance
![Page 11: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/11.jpg)
Interface vs. Abstract Class
All methods of an Interface are abstract methods while some methods of an Abstract class are abstract methods
An interface can only define constants while abstract class can have fields
Interfaces have no direct inherited relationship with any particular class, they are defined independently
![Page 12: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/12.jpg)
Interface as a Type
![Page 13: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/13.jpg)
Interface as a Type
When you define a new interface, you are defining a new reference type
You can use interface names anywhere you can use any other type name
If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface
![Page 14: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/14.jpg)
Example: Interface as a Type
Let's say Person class implements PersonInterface interface
You can do
– Person p1 = new Person();
– PersonInterface pi1 = p1;
– PersonInterface pi2 = new Person();
![Page 15: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/15.jpg)
Interface vs. Class
![Page 16: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/16.jpg)
Interface vs. Class: Commonality
Interfaces and classes are both types
– For example:
// Recommended practice
PersonInterface pi = new Person();
Interface and Class can both define methods
![Page 17: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/17.jpg)
Interface vs. Class: DifferencesThe methods of an Interface are all abstract methods
You cannot create an instance from an interface
– For example:
PersonInterface pi = new PersonInterface(); //ERROR!
An interface can only be implemented by classes or extended by other interfaces
![Page 18: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/18.jpg)
Defining Interface
![Page 19: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/19.jpg)
Defining Interface
To define an interface, we write:
public interface [InterfaceName] {
//some methods without the body
}
![Page 20: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/20.jpg)
Example
public interface Relation {
public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object b);
public boolean isEqual( Object a, Object b);
}
![Page 21: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/21.jpg)
ImplementingInterface
![Page 22: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/22.jpg)
Implementing Interfacepublic class Line implements Relation {
private double x1;
private double x2;
private double y1;
private double y2;
public Line(double x1, double x2, double y1, double y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
} // More code follows
![Page 23: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/23.jpg)
Implementing Interfacepublic double getLength(){
double length = Math.sqrt((x2-x1)*(x2-x1) +
(y2-y1)* (y2-y1));
return length;
}
public boolean isGreater( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen > bLen);}
![Page 24: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/24.jpg)
Implementing Interface
public boolean isLess( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen < bLen);
}
public boolean isEqual( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen == bLen);
}}
![Page 25: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/25.jpg)
Implementing Interface
Line.java:4: Line is not abstract and does not override abstract method
isGreater(java.lang.Object,java.lang.Object) in Relation
public class Line implements Relation
^
1 error
![Page 26: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/26.jpg)
ImplementingMultiple
Interfaces
![Page 27: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/27.jpg)
Relationship of an Interface to a Class
A sub class can only extend one super class, but it can implement multiple Interfaces
All abstract methods of all interfaces have to be implemented by the sub class
![Page 28: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/28.jpg)
Example
A sub class extends one super class but multiple Interfaces:
public class ComputerScienceStudent
extends Student
implements PersonInterface,
AnotherInterface,
Thirdinterface{
// All abstract methods of all interfaces
// need to be implemented.}
![Page 29: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/29.jpg)
Inheritance Among Interfaces
![Page 30: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/30.jpg)
Inheritance Among Interfaces
Interfaces can have inheritance relationship among themselves
public interface PersonInterface {
void doSomething();
}
public interface StudentInterface extends PersonInterface {
void doExtraSomething();
}
![Page 31: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/31.jpg)
Rewriting Interfaces
![Page 32: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/32.jpg)
Problem
Consider an interface that you have developed called DoIt:
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
}
![Page 33: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/33.jpg)
Problem…
Suppose that, at a later time, you want to add a third method to DoIt, so that the interface now becomes:
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
boolean didItWork(int i, double x, String s);
}
8/16/13
![Page 34: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/34.jpg)
Solution of Rewriting an Existing Interface
For example, you could create a DoItPlus interface that extends DoIt:
public interface DoItPlus extends DoIt {
boolean didItWork(int i, double x, String s);
}
![Page 35: Interfaces](https://reader034.fdocuments.us/reader034/viewer/2022051818/54b7b9814a7959181f8b46c4/html5/thumbnails/35.jpg)
Thank You…!