Interfaces in Java (1)
-
Upload
bhuvanesh-gogineni -
Category
Documents
-
view
223 -
download
0
Transcript of Interfaces in Java (1)
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 1/30
Interfaces in Java
Big Java - Chapter 09
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 2/30
Some more design principles …
Code reuse is one of the major concern of softwareengineering
Not only saves time in terms of development, greatly
eases software maintenance cost
Programming for interface is one of the most
important strategy for software reuse
2/13/2012 OOP 2
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 3/30
Java Interfaces - Overview
Essentially Template Classes
Serves as design guideline
Typifies a class
Sketches out the outline (“skeleton”) of the class
Recommends class designers to follow the skeleton
What do we mean by “skeleton”?
What is then the class designer’s job?
2/13/2012 OOP 3
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 4/30
The Interface “Skeleton”
Two pieces Method
Field
Method skeleton
Method signature ONLY
NO code
Should be tagged public …. (Why??)
Field skeleton
Should be tagged public …. (Why??)
Should be tagged static … (Why??)
2/13/2012 OOP 4
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 5/30
Job of the class designer
Needs to create a class
Starts finding a matching template interface
If s/he finds one then implements the interface
ALL methods of the interface now get codes!
CANNOT directly use the interface
Interfaces DO NOT have objects … (why??)
ERROR:
Measurable x = new Measurable();2/13/2012 OOP 5
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 6/30
Looking more closely …
Consider DataSet class from Chapter 06
A simple class lets you find out MAX and AVG from a
set of values
getMaximum
getAverage method
Values are added one by one to it
Let’s see the code …
2/13/2012 OOP 6
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 7/302/13/2012 OOP 7
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 8/302/13/2012 OOP 8
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 9/30
Where’s the problem?
Imagine: Joe wants to use the DataSet class for BankAccount
objects
Harry wants to use the same DataSet class for Coin
objects
Both Harry and Joe want to have same statistics
(max and average) for a set of Coin and
BankAccount objects respectively
Problem:
DataSet is NOT coded for BankAccount/Coin
It only takes in double type variables (NOT object)
2/13/2012 OOP 9
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 10/30
So what to do? - I
2/13/2012 OOP 10
• Implementation DataSet classfor BankAccount Objects
• We have to do same for Coin
class as well!
OOPS!!! ……Lot of coding!!!
SHOULD WE?? … lets see
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 11/30
So what to do? - II
2/13/2012 OOP 11
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 12/30
Then what?
What is similar between Coin and BankAccount Class? Both needs to average out their amount
Bank: Amount is balance
Coin: Amount is value
Both needs to find their maximum amount
We should not duplicate the “behavior” of the primary
class (DataSet) To find maximum
To find average
2/13/2012 OOP 12
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 13/30
Towards a good strategy…
2/13/2012 OOP 13
Look at the places where we have beenchanging the code for having DataSet
for different types of objects
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 14/30
Towards a good strategy…
2/13/2012 OOP 14
You may suggest to overload the add method for different types.
Correct! But does that solve the problem?
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 15/30
Towards a good strategy…
What are all the things DataSet needs to knowabout the object (BankAccount/Coin)?
Only some measure of its “amount” (why??)
BankAccount: amount is balance (reqd. type:
double)
Coin: amount is value (reqd. type: double)
What should the object do?:
Object (BankAccount/Coin) should know how
to return its amount (balance/value) as double
Serves the purpose of DataSet
2/13/2012 OOP 15
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 16/30
Strategy
Assume: Objects (BankAccount/Coin) support an
operation getMeasure
Gives back the amount (hopefully in double)
Type of object is Measurable
Both Coin and BankAccount objects have decided
to come under one umbrella
Why? Both gets the same treatment!! – (max, avg.)
Then what about their own distinct characteristic?
2/13/2012 OOP 16
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 17/30
Strategy – Redesign DataSet
2/13/2012 OOP 17
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 18/30
What is Measurable? A Class?
BankAccount/Coin objects what to get under oneumbrella – Measurable
But they cant!!! why??
They have their own classes
Classes characterizes them as distinct entities
Do we add a method getMeasure to DataSet , that
returns double value of its amount?
If we do so, will compiler pass following statements-DataSet bankData = new DataSet();
bankData.add( new BankAccount(10000));
2/13/2012 OOP 18
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 19/30
The big question?
We want something like a Measurable object to passon to add ()
BUT, we have BankAccount/Coin objects
How do we make BankAccount/Coin Measurable?
Let us see …
2/13/2012 OOP 19
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 20/30
The answer – Type Substitution
Compiler is lucky!! … we have Type Substitution
Type substitution:
IF compiler expects type T
and
IF variable or expression of another type S is
“permitted”
THEN it is called type substitution
Note: this notion is much more powerful than simple
coercion
Ex:
a = 5/2.0 value stored in a as double 2/13/2012 OOP 20
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 21/30
You have already seen it!!
out.println( momsSaving );
type expected: Object
Permitted class of momsSaving: BankAccount
BankAccount is subtype of Object
g2.draw( rectangle1 );
type expected: Shape
Permitted class of momsSaving: BankAccount Rectangle is subtype of Shape
What’s the lesson? …
2/13/2012 OOP 21
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 22/30
Lesson A - Sub-Typing
Example subtyping (i.e. IS-A relation) BankAccount IS-A Object
SavingAccount IS-A BankAccount
Rectangle IS-A Shape
CONDITIONS:
Needs to guarantee that sub-type (Rectangle)
supports all the operations of its super-type (Shape)
Of course subtype can provide (and normally do) moreset of operations
Interface of subtype needs to be super-set of its
super type
Guess why this requirement?2/13/2012 OOP 22
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 23/30
Getting back to the problem …
How to make BankAccount Measurable? Two things:
Create a “type” Measurable
Make BankAccount as subtype of Measurable
But how??
Types are created by class and interface
constructs
Subtype are created by (two ways): Extending a Class: If class A extends class B,
then A is subtype of B
Implementing Interfaces: If class A implements
interface I, then A is subtype of I2/13/2012 OOP 23
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 24/30
End of sad days?
Well actually not!! How to decide which way to take:
Extending classes?
Implementing interfaces?
Imagine:
A situation where you really do not want to create
objects of the super-type
BUT you need to create objects of the sub-type Do we want Measurable objects? NO!!
Do we want BankAccount/Coin objects? YES!!
2/13/2012 OOP 24
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 25/30
Java Interfaces - I
Java Interface is just a set of operation specifications
Just the same as creating classes except
We do not (and actually cannot!!) provide
implementation (i.e. code) of any of method
Any class providing implementation of that interface
becomes subtype of the interface type
2/13/2012 OOP 25
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 26/30
Java Interfaces - II
Filename has to be same as of interface
Just like classes
Ex: interface Measurable needs to be stored in file Measurable.java
Public interfaces are available outside of the package inwhich they have been defined
Non-public interfaces have package level access
Methods of interface are always public – why??
A class implementing an interface necessarily need toprovide implementation for ALL methods of the
interface2/13/2012 OOP 26
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 27/30
2/13/2012 OOP 27
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 28/30
2/13/2012 OOP 28
8/3/2019 Interfaces in Java (1)
http://slidepdf.com/reader/full/interfaces-in-java-1 29/30
2/13/2012 OOP 29