27
03/16/22 03/16/22 Abstract classes & Abstract classes & Interface Interface 1 Object Oriented Object Oriented Design and Design and Programming II Programming II Chapter 10 Chapter 10 Abstract classes Abstract classes and Interfaces and Interfaces

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

Embed Size (px)

DESCRIPTION

Object Oriented Design and Programming II Chapter 10 Abstract 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 - PowerPoint PPT Presentation

Citation preview

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 11

Object Oriented Design Object Oriented Design and Programming II and Programming II

Chapter 10Chapter 10Abstract classes and Abstract classes and

InterfacesInterfaces

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

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 44

abstract and final Methodsabstract and final Methods An abstract method in a superclass has no An abstract method in a superclass has no

implementation, and it is to be overridden implementation, and it is to be overridden by a method in its subclass.by a method in its subclass.

A final method in a superclass cannot be A final method in a superclass cannot be overridden at its subclass.overridden at its subclass.

Why do I need to bother with abstract Why do I need to bother with abstract methods and final methods? Can I live methods and final methods? Can I live happily in Java without them?happily in Java without them?

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 55

Abstract ClassAbstract Class A class that cannot instantiate objects.A class that cannot instantiate objects.

Message

TextMessage

VoiceMessage

FaxMessage

Public abstract class Message {}

Do I and should I use non-abstract methods in abstract classes?

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 66

Abstract or not-abstract Abstract or not-abstract Contains Contains abstractabstract

methodsmethods

Contains non-Contains non-abstract abstract methodsmethods

Class abstractClass abstract May contain May contain abstract methods. abstract methods. abstract methods abstract methods must be in abstract must be in abstract classesclasses

GeometricObject inGeometricObject in

BJ_GeomObj. BJ_GeomObj. Situation 3Situation 3

OKOK

E.g. class E.g. class BJ_abstract_FigureBJ_abstract_Figure

Class non-Class non-abstractabstract

Not allowedNot allowed Fine. You are Fine. You are familiar with thisfamiliar with this

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 77

Example BJ_abstract_FigureExample BJ_abstract_Figure

Problems of Bj_FindAreaProblems of Bj_FindArea BJ_abstract_FigureBJ_abstract_Figure

An abstract class used as supertypeAn abstract class used as supertype An object cannot be created from an abstract An object cannot be created from an abstract

classclass An abstract class can be extended by a An abstract class can be extended by a

subclasssubclass

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 88

Example BJ_abstract_Figure2Example BJ_abstract_Figure2

An abstract class used as supertypeAn abstract class used as supertype An object cannot be created from an An object cannot be created from an

abstract classabstract class An array of the abstract type is used to An array of the abstract type is used to

contain objects of the concrete subclassescontain objects of the concrete subclasses

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 99

Example BJ_GeomObj_Circle9Example BJ_GeomObj_Circle9

Circle9 extends an abstract class Circle9 extends an abstract class GeometricObjectGeometricObject

Note the 4 situations in the project:Note the 4 situations in the project: Circle class has concrete method getArea()Circle class has concrete method getArea() No abstract method getArea(); concrete No abstract method getArea(); concrete

getArea() in CirclegetArea() in Circle abstract method getArea(); concrete abstract method getArea(); concrete

getArea() in CirclegetArea() in Circle Instantiate object of abstract classInstantiate object of abstract class

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1010

Empty vs abstract methodsEmpty vs abstract methods

Method with empty bodyMethod with empty body protected abstract double getArea();protected abstract double getArea();

Abstract methodAbstract method protected abstract double getArea();protected abstract double getArea();

From abstract class

To Interface

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1212

What is InterfaceWhat is Interface An interface is a named collection of method An interface is a named collection of method

definitions and constants definitions and constants ONLYONLY. . An interface defines a protocol of behavior that An interface defines a protocol of behavior that

can be implemented by any class anywhere in can be implemented by any class anywhere in the class hierarchy. the class hierarchy.

An interface An interface defines defines a set of methods but does a set of methods but does not not implementimplement them. them.

A class that implements the interface agrees to A class that implements the interface agrees to implement implement allall the methods defined in the the methods defined in the interface, thereby agreeing to certain behaviors. interface, thereby agreeing to certain behaviors.

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1313

Interface and Abstract ClassesInterface and Abstract Classes

An interface cannot implement any An interface cannot implement any methods, whereas an abstract class can. methods, whereas an abstract class can.

A class can implement many interfaces A class can implement many interfaces but can have only one superclass. but can have only one superclass.

An interface is not part of the class An interface is not part of the class hierarchy. Unrelated classes can hierarchy. Unrelated classes can implement the same interface. implement the same interface.

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1414

Multiple InheritanceMultiple InheritanceClass 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.

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1515

Defining InterfacesDefining Interfaces

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1616

Interface DeclarationInterface Declaration

public interface StockWatcher{ }

public interface Sortable{ }

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1717

Interface BodyInterface Body The interface body contains method The interface body contains method

declarations for declarations for ALLALL the methods included the methods included in the interface. in the interface.

A method declaration within an interface is A method declaration within an interface is followed by a semicolon (;) because an followed by a semicolon (;) because an interface does not provide interface does not provide implementations for the methods declared implementations for the methods declared within it. within it.

All methods declared in an interface are All methods declared in an interface are implicitly implicitly publicpublic and and abstractabstract. .

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1818

Implement an InterfaceImplement an Interface

An interface defines a protocol of An interface defines a protocol of behavior. behavior.

A class that implements an interface A class that implements an interface adheres to the protocol defined by that adheres to the protocol defined by that interface. interface.

To declare a class that implements an To declare a class that implements an interface, include an implements clause in interface, include an implements clause in the class declaration. the class declaration.

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 1919

Implement Interface (Example)Implement Interface (Example)public class StockApplet extends Applet implements StockWatcher { ...

public void valueChanged(String tickerSymbol, double newValue) { if (tickerSymbol.equals(sunTicker)) { // record newValue for sunTicker... } else if (tickerSymbol.equals(oracleTicker)) { // record newValue for oracleTicker } else if (tickerSymbol.equals(ciscoTicker)) { // record newValue for ciscoTicker } }}

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 2020

Code ReviewCode Review

BJ_InterfaceBJ_Interface Objects inheriting properties of superclass Objects inheriting properties of superclass

and implementing properties of interfaceand implementing properties of interface A class may have only one superclass but A class may have only one superclass but

may implement multiple interfacesmay implement multiple interfaces Using an array of supertypeUsing an array of supertype polymorphismpolymorphism

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 2121

SortingSorting It is easy to write a sorting method for numbers of It is easy to write a sorting method for numbers of

a specific type.a specific type. bubblesort, shellsort, quicksort, heapsort.bubblesort, shellsort, quicksort, heapsort.

It is not easy to write a method to sort numbers of It is not easy to write a method to sort numbers of any any primitive type: short, int, long, float, primitive type: short, int, long, float, andand double.double. See Example 9.2 GenericSort.javaSee Example 9.2 GenericSort.java

It is a challenge to write a method to sort It is a challenge to write a method to sort objectsobjects How do you do the above in C?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 DemoSource Code Review and Demo

Exercise 1: Download, run, and study Exercise 1: Download, run, and study BJ_Sort/SortTest.javaBJ_Sort/SortTest.java

Each class needs to implement the Sortable Each class needs to implement the Sortable interface with a compare() methodinterface with a compare() method

Exercise 2: Add other sorting methodsExercise 2: Add other sorting methodsBubble, Insertion, SelectionBubble, Insertion, Selection

Exercise 3: C:\ProgramFiles\Java\ jdk1.5.0_12\Exercise 3: C:\ProgramFiles\Java\ jdk1.5.0_12\demo\applets\SortDemodemo\applets\SortDemo To compare speedTo compare speed

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 2424

Application Programming Application Programming Interface (API)Interface (API)

How do I learn to use the packages in the How do I learn to use the packages in the Java platform?Java platform?

Answer: APIAnswer: API http://java.sun.com/j2se/1.5.0/docs/api/http://java.sun.com/j2se/1.5.0/docs/api/

04/19/2304/19/23 Abstract classes & InterfaceAbstract classes & Interface 2525

API StudyAPI Studyexample: StringTokenizerexample: StringTokenizer

1.1. Location of the API( package)Location of the API( package)2.2. Class definitionClass definition3.3. Constructor(s) – usually more Constructor(s) – usually more

than onethan one4.4. Methods (you can see public only)Methods (you can see public only)5.5. Variables (any public ones)Variables (any public ones)6.6. Interfaces – supply your own Interfaces – supply your own

methodsmethods7.7. ExceptionsExceptions8.8. ExamplesExamples

Clone()

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()

hashCode() returns the object’s 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