22
Abstract Classes and Interfaces Chapter 9 CSCI 1302

Abstract Classes and Interfaces Chapter 9 CSCI 1302

Embed Size (px)

Citation preview

Page 1: Abstract Classes and Interfaces Chapter 9 CSCI 1302

Abstract Classes and Interfaces

Chapter 9CSCI 1302

Page 2: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 2

Outline

• Introduction• Abstract Classes• Interfaces

– Implementing Interfaces– Interfaces vs. Abstract Classes– Creating Custom Interfaces

• Processing Primitive Data Type Values as Objects– Numeric Wrapper Class Constructors

Page 3: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 3

Outline

• Processing Primitive Data Type Values as Objects (cont.)– Numeric Wrapper Class Constants– Conversion Methods– The Static valueOf Methods– The Methods for Parsing Strings into

Numbers

• Automatic Conversion Between Primitive and Wrapper Class Types

Page 4: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 4

Introduction

• Recall specificity chain• Superclasses should contain common

features of its subclasses• Superclass so general it does not have

specific instances is an abstract class• Java classes can have only one

superclass (single inheritance)• Can emulate multiple inheritance with

interfaces

Page 5: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 5

Abstract Classes

• Concrete classes vs. abstract classes• Classes that are derived from the same

superclass have common properties and behaviors

• Often the superclass will have methods (behaviors) that it cannot and should not implement for its subclasses

• These methods are abstract methods, method signature in the superclass, but only implemented in the subclass

Page 6: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 6

Abstract Classes

• A class that contains abstract methods should be declared abstract

• Cannot declare new instances of this class

• Provides common features (data and methods)

• Declares subclass implementation-specific methods as abstract

Page 7: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 7

Abstract Classes

• Abstract methods must be in abstract classes

• If a subclass does not implement all abstract superclass methods, the subclass must be abstract

• Abstract classes cannot be instantiated, but can still have constructors for its subclasses

Page 8: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 8

Abstract Classes

• See GeometricObject code packet

Circle -radius: double +getRadius(): double +setRadius(radius: double): void

Cylinder

-length: double +getLength(): double +setLength(length: double): void +findVolume(): double

GeometricObject -color: String -filled: boolean +getColor(): String +setColor(String color): void +isFilled(): boolean +setFilled(boolean filled): void +findArea(): double +findPerimeter(): double

Object

Rectangle -width: double -length: double +getWidth(): double +setWidth(width: double): void +getLength(): double +setLength(length: double): void

UML Notation: The abstract class name and the abstract method names are italicized.

Page 9: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 9

Interfaces

• Classlike construct that contains only constants and abstract methods

• Declared with interface keywordpublic interface InterfaceName {/** Constants *//** Method signatures */

}

• Provides a generic framework for common operations

• Best example: java.lang.Comparable

Page 10: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 10

Interfaces

• Interface can define generic compareTo methodpackage java.lang;public interface Comparable {public int compareTo(Object o);

}

• Defined behavior: Returns 0 if equal, negative number if calling object is less than o, positive number if calling object is greater than o

Page 11: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 11

Interfaces

• Can now create generic max methodpublic Object max(Object o1, Object o2) {if(((Comparable)o1).compareTo(o2) > 0)

return o1;else

return o2;}

Page 12: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 12

Implementing Interfaces

• Use keyword implements when forming class

• Inherits all constants in the interface and implements all the methods

• Another form of generic programming

Page 13: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 13

Implementing Interfaces

• Create a ComparableRectangle class that implements Comparable

• See ComparableRectangle.java

Rectangle -

GeometricObject -

java.lang.Comparable +compareTo(o: Object):int

Notation: The interface name and the method names are italicized. The dashed lines and hollow triangles are used to point to the interface.

ComparableRectangle -

Page 14: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 14

Interface vs. Abstract Classes

• Interfaces and abstract classes can be used in similar ways, but are defined differently

• In an interface, the data must be constants, abstract classes can have non-constant data fields (variables)

• Interface methods have only a signature, abstract classes can have concrete methods

Page 15: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 15

Interface vs. Abstract Classes

• Classes can only inherit from one class, but can implement manypublic class NewClass extends BaseClassimplements Interface1, … , InterfaceN

• Both model common features, when do you use one over the other?

• Use inheritance when modeling strong is-a relationships. Use interfaces when modeling weak is-a relationships

• Interfaces are adjectives or nouns

Page 16: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 16

Primitive Data Types as Objects

• Primitive data types exist for performance issues

• Some methods only take objects, use wrapper classes to wrap primitive data types into forms useful for generic programming

• Boolean, Character, Double, Float, Byte, Short, Integer, and Long in java.lang

• All extend the abstract Number class and implement the Comparable interface

Page 17: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 17

Wrapper Class Constructors

• Construct from primitive type or stringInteger intObj = new Integer(5);Integer intObj = new Integer(“5”);Double dblObj = new Double(5.0);Double dblObj = new Double(“5.0”);

Page 18: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 18

Wrapper Class Constants

• All wrapper classes have MAX_VALUE and MIN_VALUE

System.out.println(Integer.MAX_VALUE);System.out.println(Float.MIN_VALUE);

Page 19: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 19

Conversion Methods• doubleValue, floatValue, intValue, longValue, and shortValue methods are available in all wrapper classes

long l = doubleObject.longValue();int i = integerObject.intValue();

double a = 5.9;Double doubleObject = new Double(a);String s = doubleObject.toString();

Page 20: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 20

Static valueOf Methods

• Convert string into object of wrapper type

Double a = Double.valueOf(“12”);Integer b = Integer.valueOf(“4”);

Page 21: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 21

Parsing Strings into Numbers

• Parse strings into with parseType methods

Integer.parseInt(“10”);Long.parseLong(“12”);Byte.parseByte(“14”);

Page 22: Abstract Classes and Interfaces Chapter 9 CSCI 1302

CSCI 1302 – Abstract Classes and Interfaces 22

Sort Arrays of Objects

• See GenericSort.java