Title: CSC 243
1CSC 243 Java Programming, Spring, 2009
- Week 5, Interfaces, Derived Classes, and Abstract
Classes
2Interface Inheritance
- parson/JavaLang/sequence_adt
- An interface specifies operations as method
signatures with no implementation bodies. - A method signature consists of a methods 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.
3sequence_interface (ADT) sequence of string
objects
- cp parson/JavaLang/sequence_adt
/JavaLang/sequence_adt - ltltinterfacegtgt sequence_interface.java
- sequence_interface
- insert(value string, offset integer)
boolean - remove(offset integer) boolean
- get(offset integer) string
- size() integer
4Concrete class sequence_arrayimpl implements
sequence_interface
- ltltinterfacegtgt sequence_interface
- sequence_arrayimpl insert(), remove()
- get(), size()
- private String array
- private int count // number of array elements
actually used
5Class sequence_linkedlistimpl implements
sequence_interface
- ltltinterfacegtgt 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
6Linked 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.
7Tail 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
8Disadvantages 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
9Advantages 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 sub-sequences. - Private operations can manipulate nodes directly.
10Class sequence_arraylistimpl implements
sequence_interface
- ltltinterfacegtgt sequence_interface
- sequence_arraylistimpl insert(), remove()
- get(), size()
-
- private ArrayListltStringgt stringVector new
ArrayListltStringgt()
11Modular 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
12Object oriented designInterface 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.
13Concrete class sequence_arrayimpl implements
sequence_interface
- ltltinterfacegtgt sequence_interface
- sequence_arrayimpl
- sequence_linkedlistimpl
- Polymorphism here means that one interface can
take the form of multiple implementation classes.
14Implementation 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.
15Package sequence_aclass class sequence_abstract_ba
seclass
- ltltinterfacegtgt sequence_interface
- ltltabstractgtgt 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