Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.

Similar presentations


Presentation on theme: "CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes."— Presentation transcript:

1 CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes

2 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

3 sequence_interface (ADT) – sequence of string objects cp ~parson/JavaLang/sequence_adt.zip ~/JavaLang/sequence_adt > sequence_interface.java sequence_interface insert(value : string, offset : integer) : boolean remove(offset : integer) : boolean get(offset : integer) : string size() : integer

4 Concrete class sequence_arrayimpl implements sequence_interface > sequence_interface sequence_arrayimpl insert(), remove() get(), size() private String [] array ; private int count ; // number of array elements actually used

5 Class sequence_linkedlistimpl implements sequence_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

6 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.

7 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 :=

8 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

9 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 sub- sequences. Private operations can manipulate nodes directly.

10 Class sequence_arraylistimpl implements sequence_interface > sequence_interface sequence_arraylistimpl insert(), remove() get(), size() private ArrayList stringVector = new ArrayList ();

11 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

12 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.

13 Concrete class sequence_arrayimpl implements sequence_interface > sequence_interface sequence_arrayimpl sequence_linkedlistimpl Polymorphism here means that one interface can take the form of multiple implementation classes.

14 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.

15 Package sequence_aclass class sequence_abstract_baseclass > sequence_interface > 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


Download ppt "CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes."

Similar presentations


Ads by Google