Lecture slides

Download Report

Transcript Lecture slides

CSC 243 – Java Programming,
Spring, 2014
Week 4, Interfaces, Derived Classes,
and Abstract Classes
Interface Inheritance
• ~parson/JavaLang/sequence_adt
• An interface specifies operations as method
signatures with no implementation bodies.
• A method signature consists of a method’s name,
parameter types, return type and exceptions.
• An interface may also specific final constant values
similar to class fields.
• All operations and constants are public.
• cp ~ parson/JavaLang/sequence_adt.zip ~/JavaLang/sequence_adt.zip
• cd ~/JavaLang ; unzip sequence_adt.zip ; cd sequence_adt
• gmake clean test
sequence_interface (ADT) –
sequence of string objects
cp ~parson/JavaLang/sequence_adt.zip ~/JavaLang/sequence_adt
<<interface>>
sequence_interface
sequence_interface.java
insert(value : string, offset : integer) : boolean
remove(offset : integer) : boolean
get(offset : integer) : string
size() : integer
Concrete class sequence_arrayimpl
implements sequence_interface
<<interface>> sequence_interface
sequence_arrayimpl
insert(), remove()
get(), size()
private String [] array ;
private int count ;
actually used
// number of array elements
Class sequence_linkedlistimpl
implements sequence_interface
<<interface>> sequence_interface
sequence_linkedlistimpl
insert(), remove()
get(), size()
private static class listelement { // private local helper class
String content ; // string stored at this element
listelement next ; // next element, higher in number than this one
}
private listelement first ;
private int count ;
// number of linked list nodes actually used
Linked Lists
contents next contents next contents next
head
tail
There is a pointer variable or member data field,
external to the list, that points to the head of
the list.
This pointer is null when the list is empty.
The tail of the list has a null next pointer.
Tail recursive definitions of a List
• List := Node | Node List
•
•
•
•
What problem results from this definition?
What kind of List can you NOT represent?
How might you fix this problem?
Hint: Restate above as List := [Node] | [Node] + List
• List :=
Disadvantages of exposing
implementation to client code
• Suppose you give client code direct access to
the listelement linked list, so that any client
pointer to a listelement is a sequence.
• How must you implement negative offsets and
test non-negative offsets for validity?
contents next contents next contents next
start of a list start of a different list
Advantages of information hiding
and data abstraction
contents next contents next contents next
start of the list (first),
count of nodes
Abstracting the sequence and hiding the linked list lets
you use count to determine offset validity without
searching the list.
You can still provide client code with the advantages of
recursive lists by creating operations to clone subsequences.
Private operations can manipulate nodes directly.
Class sequence_arraylistimpl
implements sequence_interface
<<interface>> sequence_interface
sequence_arraylistimpl
insert(), remove()
get(), size()
private ArrayList<String> stringVector = new ArrayList<String>();
Modular design
(object based design)
• Information hiding
• By hiding class implementation code and data structure
from client code applications, and hiding client
implementation details from class implementation
code, it is possible to change them semi-independently.
• Encapsulation
• Basic language mechanisms include public, package,
protected and private access control.
• Modularity – “plug and play” with compiled modules
• Reuse – library modules can be used in many apps
Object oriented design
Interface inheritance
• A Java interface describes operations and
constants of a set of Java classes without
implementing any of those operations
•
•
•
•
All methods and data are public. All data are final.
There is no code for any of the methods.
The interface specifies a contract.
Operation preconditions state requirements on client
code before a method call.
• Operation postconditions state the conditions met by
the called method, if the preconditions have been met.
Concrete class sequence_arrayimpl
implements sequence_interface
<<interface>> sequence_interface
sequence_arrayimpl
sequence_linkedlistimpl
Polymorphism here means that one interface can take
the form of multiple implementation classes.
Implementation Inheritance
• ~parson/JavaLang/sequence_aclass
• Fields and methods common to multiple peer
classes go into a common base class.
• This base class is abstract if it cannot
construct fully formed objects by itself. An
abstract class requires one or more derived
classes for construction.
• Base class methods may include protected
helper methods.
Package sequence_aclass class
sequence_abstract_baseclass
<<interface>> sequence_interface
<<abstract>> sequence_abstract_baseclass
size() : int
protected int getRealOffset(int offset, boolean isinsert) : int
(helper method to convert an offset parameter to a real offset)
protected int count
sequence_arrayimpl sequence_linkedlistimpl sequence_arraylistimpl