Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces - PowerPoint PPT Presentation


PPT – Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces PowerPoint presentation | free to download - id: 4d8e11-YzRlY


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces


Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces * Abstract classes & Interface * Software Engineering Principle Minimize change ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 28
Provided by: uwoshEduf
Learn more at: http://www.uwosh.edu


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces

Object Oriented Design and Programming II Chapter
10Abstract classes and Interfaces
Software Engineering Principle
  • Minimize change

From concrete classes
  • To abstract classes
  • To interface
  • Examples
  • BJ_1_initial_SalCal
  • BJ_2_class_SalCal
  • BJ_3_abstract_SalCal
  • BJ_4_abstract_method_SalCal
  • BJ_5_interface_SalCal

abstract and final Methods
  • An abstract method in a superclass has no
    implementation, and it is to be overridden by a
    method in its subclass.
  • A final method in a superclass cannot be
    overridden at its subclass.
  • Why do I need to bother with abstract methods and
    final methods? Can I live happily in Java
    without them?

Abstract Class
  • A class that cannot instantiate objects.

Text Message
Voice Message
Fax Message
Public abstract class Message
Do I and should I use non-abstract methods in
abstract classes?
Abstract or not-abstract
Contains abstract methods Contains non-abstract methods
Class abstract May contain abstract methods. abstract methods must be in abstract classes GeometricObject in BJ_GeomObj. Situation 3 OK E.g. class BJ_abstract_Figure
Class non-abstract Not allowed Fine. You are familiar with this
Example BJ_abstract_Figure
  • Problems of Bj_FindArea
  • BJ_abstract_Figure
  • An abstract class used as supertype
  • An object cannot be created from an abstract
  • An abstract class can be extended by a subclass

Example BJ_abstract_Figure2
  • An abstract class used as supertype
  • An object cannot be created from an abstract
  • An array of the abstract type is used to contain
    objects of the concrete subclasses

Example BJ_GeomObj_Circle9
  • Circle9 extends an abstract class GeometricObject
  • Note the 4 situations in the project
  • Circle class has concrete method getArea()
  • No abstract method getArea() concrete getArea()
    in Circle
  • abstract method getArea() concrete getArea() in
  • Instantiate object of abstract class

Empty vs abstract methods
  • Method with empty body
  • protected abstract double getArea()
  • Abstract method
  • protected abstract double getArea()

From abstract class
  • To Interface

What is Interface
  • An interface is a named collection of method
    definitions and constants ONLY.
  • An interface defines a protocol of behavior that
    can be implemented by any class anywhere in the
    class hierarchy.
  • An interface defines a set of methods but does
    not implement them.
  • A class that implements the interface agrees to
    implement all the methods defined in the
    interface, thereby agreeing to certain behaviors.

Interface and Abstract Classes
  • An interface cannot implement any methods,
    whereas an abstract class can.
  • A class can implement many interfaces but can
    have only one superclass.
  • An interface is not part of the class hierarchy.
    Unrelated classes can implement the same

Multiple Inheritance
Class A
Class B
Class C
Class ABC
Class ABC inherits all variables and methods from
Class A, Class B, and Class C. Java does NOT
support multiple inheritances. However, you can
use interface to implement the functionality of
multiple inheritance.
Defining Interfaces
Interface Declaration
public interface StockWatcher public
interface Sortable
Interface Body
  • The interface body contains method declarations
    for ALL the methods included in the interface.
  • A method declaration within an interface is
    followed by a semicolon () because an interface
    does not provide implementations for the methods
    declared within it.
  • All methods declared in an interface are
    implicitly public and abstract.

Implement an Interface
  • An interface defines a protocol of behavior.
  • A class that implements an interface adheres to
    the protocol defined by that interface.
  • To declare a class that implements an interface,
    include an implements clause in the class

Implement Interface (Example)
public class StockApplet extends Applet
implements StockWatcher ...
public void valueChanged(String tickerSymbol,
double newValue) if
// record newValue for sunTicker...
else if (tickerSymbol.equals(oracleTi
cker)) // record newValue for
oracleTicker else if
(tickerSymbol.equals(ciscoTicker)) //
record newValue for ciscoTicker

Code Review
  • BJ_Interface
  • Objects inheriting properties of superclass and
    implementing properties of interface
  • A class may have only one superclass but may
    implement multiple interfaces
  • Using an array of supertype
  • polymorphism

  • It is easy to write a sorting method for numbers
    of a specific type.
  • bubblesort, shellsort, quicksort, heapsort.
  • It is not easy to write a method to sort numbers
    of any primitive type short, int, long, float,
    and double.
  • See Example 9.2 GenericSort.java
  • It is a challenge to write a method to sort
  • How do you do the above in C?

Example Comparable interface
  • Java.lang.Comparable
  • See BJ_Max
  • See BJ_GenericSort
  • Still relying on Java 1.5 unboxing feature of
    wrapper objects

Source Code Review and Demo
  • Exercise 1 Download, run, and study
  • Each class needs to implement the Sortable
    interface with a compare() method
  • Exercise 2 Add other sorting methodsBubble,
    Insertion, Selection
  • Exercise 3 C\ProgramFiles\Java\
  • To compare speed

Application Programming Interface (API)
  • How do I learn to use the packages in the Java
  • Answer API
  • http//java.sun.com/j2se/1.5.0/docs/api/

API Studyexample StringTokenizer
  1. Location of the API( package)
  2. Class definition
  3. Constructor(s) usually more than one
  4. Methods (you can see public only)
  5. Variables (any public ones)
  6. Interfaces supply your own methods
  7. Exceptions
  8. Examples

  • newObject someObject
  • Only assigns the reference of someObject to
    newObject. No copy is made
  • newObject someObject.clone()
  • Copies someObject to a new memory location
  • See BJ_House

  • hashCode() returns the objects hash code
  • Hash code is an integer to store the object in a
    hash set.
  • If you override the equal() method, you should
    also override the hashCode
  • See BJ_House
About PowerShow.com