Your SlideShare is downloading. ×
0
Interfaces in JAVA
By Jai Vikas Marathe
Agenda
What is Interface?
Why Interface?
Interface as a Type
Interface vs. Class
Defining an Interface
Implementing ...
What is an Interface?
What is an Interface?
It defines a standard and public way of specifying the
behaviour of classes
All methods of an inte...
Example: Interface
// Note that Interface contains just set of
method
// signatures without any implementations.
// No nee...
Example 2: Interface
public interface OperateCar {
// constant declarations, if any
// method signatures
int turn(Directio...
Why Interface?
Why do we use Interfaces?
Reason #1
To reveal an object's programming interface
(functionality of the object) without rev...
Why do we use Interfaces?
Reason #2
To have unrelated classes implement similar methods
(behaviours)
Example:
Class Line...
Why do we use Interfaces?
Reason #3
To model multiple inheritance
Interface vs. Abstract Class
All methods of an Interface are abstract methods while
some methods of an Abstract class are...
Interface as a
Type
Interface as a Type
When you define a new interface, you are defining a
new reference type
You can use interface names a...
Example: Interface as a Type
Let's say Person class implements PersonInterface
interface
You can do
– Person p1 = new Pe...
Interface vs. Class
Interface vs. Class:
Commonality
Interfaces and classes are both types
– For example:
// Recommended practice
PersonInte...
Interface vs. Class:
DifferencesThe methods of an Interface are all abstract methods
You cannot create an instance from ...
Defining Interface
Defining Interface
To define an interface, we write:
public interface [InterfaceName] {
//some methods without the body
}
Example
public interface Relation {
public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object...
Implementing
Interface
Implementing Interface
public class Line implements Relation {
private double x1;
private double x2;
private double y1;
pr...
Implementing Interface
public double getLength(){
double length = Math.sqrt((x2-x1)*(x2-x1) +
(y2-y1)* (y2-y1));
return le...
Implementing Interface
public boolean isLess( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Li...
Implementing Interface
Line.java:4: Line is not abstract and does not
override abstract method
isGreater(java.lang.Object,...
Implementing
Multiple
Interfaces
Relationship of an Interface to
a Class
A sub class can only extend one super class, but it can
implement multiple Interf...
Example
A sub class extends one super class but multiple
Interfaces:
public class ComputerScienceStudent
extends Student
...
Inheritance
Among Interfaces
Inheritance Among Interfaces
Interfaces can have inheritance relationship among
themselves
public interface PersonInterfa...
Rewriting Interfaces
Problem
Consider an interface that you have developed called DoIt:
public interface DoIt {
void doSomething(int i, double ...
Problem…
Suppose that, at a later time, you want to add a third
method to DoIt, so that the interface now becomes:
public ...
Solution of Rewriting an
Existing Interface
For example, you could create a DoItPlus interface that
extends DoIt:
public ...
Thank You…!
Upcoming SlideShare
Loading in...5
×

Interfaces

436

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
436
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Interfaces"

  1. 1. Interfaces in JAVA By Jai Vikas Marathe
  2. 2. 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
  3. 3. What is an Interface?
  4. 4. 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
  5. 5. 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);
  6. 6. Example 2: Interface public 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);
  7. 7. Why Interface?
  8. 8. Why do we use Interfaces? Reason #1 To reveal an object's programming interface (functionality of the object) without revealing its implementation
  9. 9. 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)
  10. 10. Why do we use Interfaces? Reason #3 To model multiple inheritance
  11. 11. 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
  12. 12. Interface as a Type
  13. 13. 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
  14. 14. 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();
  15. 15. Interface vs. Class
  16. 16. 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
  17. 17. Interface vs. Class: DifferencesThe 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
  18. 18. Defining Interface
  19. 19. Defining Interface To define an interface, we write: public interface [InterfaceName] { //some methods without the body }
  20. 20. 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); }
  21. 21. Implementing Interface
  22. 22. Implementing Interface public 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
  23. 23. Implementing Interface public 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);}
  24. 24. 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); }}
  25. 25. 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
  26. 26. Implementing Multiple Interfaces
  27. 27. 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
  28. 28. 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.}
  29. 29. Inheritance Among Interfaces
  30. 30. Inheritance Among Interfaces Interfaces can have inheritance relationship among themselves public interface PersonInterface { void doSomething(); } public interface StudentInterface extends PersonInterface { void doExtraSomething(); }
  31. 31. Rewriting Interfaces
  32. 32. Problem Consider an interface that you have developed called DoIt: public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); }
  33. 33. 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
  34. 34. 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); }
  35. 35. Thank You…!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×