1 of 33

CISC 181

Abstract classes & interfaces

Prof. Christopher Rasmussen

April 6, 2015

2 of 33

News

  • Lab #7 today
  • Midterms will be returned on Wednesday

3 of 33

Derived Classes

  • Object-oriented languages such as Java allow classes to be derived from each other
  • So we say the more specific Fish class is based on the more general Animal class, but it "extends" or "modifies" it in some way

public class Animal { … }

public class Fish extends Animal { … }

4 of 33

instanceof

  • Test whether an object o comes from a certain class A, or from a class derived from class B, with boolean instanceof operator

if (o instanceof A) { … }

5 of 33

null

  • null can be assigned to reference variables to indicate that they are not associated with any object
    • Alternative to new when you are getting a "variable might not have been initialized" error

6 of 33

null

  • null can be assigned to reference variables to indicate that they are not associated with any object
    • Alternative to new when you are getting a "variable might not have been initialized" error
  • null has no type -- any variable that is null will return false for all instanceof tests

7 of 33

Abstract class

  • Guides design of subclasses through members, but no object can be instantiated from it
    • Opposite of "concrete" class, which is what we know about so far

8 of 33

Example: JComponent

  • A Swing JFrame can contain all kinds of GUI elements
  • The types of components are organized hierarchically

abstract

9 of 33

Abstract class

  • Guides design of subclasses through members, but no object can be instantiated from it
    • Opposite of "concrete" class, which is what we know about so far
  • Use keyword abstract before class

10 of 33

Abstract class

  • Guides design of subclasses through members, but no object can be instantiated from it
    • Opposite of "concrete" class, which is what we know about so far
  • Use keyword abstract before class
  • abstract method
    • Empty body -- for example:

abstract void methodName( … );

11 of 33

Abstract class

  • Guides design of subclasses through members, but no object can be instantiated from it
    • Opposite of "concrete" class, which is what we know about so far
  • Use keyword abstract before class
  • abstract method
    • Empty body -- for example:

abstract void methodName( … );

    • Must be in abstract class

12 of 33

Abstract class

  • Guides design of subclasses through members, but no object can be instantiated from it
    • Opposite of "concrete" class, which is what we know about so far
  • Use keyword abstract before class
  • abstract method
    • Empty body -- for example:

abstract void methodName( … );

    • Must be in abstract class
    • Must be overridden and defined in subclass

13 of 33

Example: Shape

abstract class Shape {

abstract double computeArea();

}

14 of 33

Example: Shape

abstract class Shape {

abstract double computeArea();

}

class Circle extends Shape {

double computeArea()

{ return Math.PI * radius * radius; }

}

15 of 33

Example: Shape

abstract class Shape {

abstract double computeArea();

}

class Circle extends Shape {

double computeArea()

{ return Math.PI * radius * radius; }

}

class Rectangle extends Shape {

double computeArea()

{ return width * height; }

}

16 of 33

Abstract classes & polymorphism

  • Recall that you can put Fish (subclass) objects into an ArrayList of Animal (superclass) objects and Java "knows" which is which
    • You can always assign a subclass object to a superclass-type variable, but not vice versa...why?

17 of 33

Abstract classes & polymorphism

  • Recall that you can put Fish (subclass) objects into an ArrayList of Animal (superclass) objects and Java "knows" which is which
    • You can always assign a subclass object to a superclass-type variable, but not vice versa...why?
  • Convenient to have ArrayList where element type is abstract base class

18 of 33

interfaces

  • An interface is like an abstract class, with some key differences

19 of 33

interfaces

  • An interface is like an abstract class, with some key differences
  • Syntax
    • Use interface instead of class in definition
    • Use implements instead of extends to associate a class with an interface

20 of 33

interfaces

  • An interface is like an abstract class, with some key differences
  • Syntax
    • Use interface instead of class in definition
    • Use implements instead of extends to associate a class with an interface
  • Properties
    • All methods are implicitly public and abstract
      • So make your overrides public
    • Class can implement multiple interfaces -- put in comma-separated list:

class A implements I1, I2 { … }

21 of 33

More about interfaces

  • Can also use instanceof to test whether object implements a certain interface

22 of 33

More about interfaces

  • Can also use instanceof to test whether object implements a certain interface
  • interfaces can't implement other interfaces

23 of 33

More about interfaces

  • Can also use instanceof to test whether object implements a certain interface
  • interfaces can't implement other interfaces
  • All fields implicitly static & final

24 of 33

More about interfaces

  • Can also use instanceof to test whether object implements a certain interface
  • interfaces can't implement other interfaces
  • All fields implicitly static & final
  • Can declare default methods with bodies which are shared by implementing classes
    • This allows you to add a method to an existing interface without "breaking" implementations
    • Can still be overridden

25 of 33

Example interface: Comparable<T>

  • Defines "total ordering" on class objects
  • Has one method: int compareTo(T)
    • Remember: returns 0 if equal, negative if less than, positive if greater than

26 of 33

Example interface: Comparable<T>

  • Defines "total ordering" on class objects
  • Has one method: int compareTo(T)
    • Remember: returns 0 if equal, negative if less than, positive if greater than
  • Necessary to implement for your objects to be sortable by Java Collections sort()

27 of 33

Limitations of Comparable<T>

  • Must have access to class T to make it implement the interface

28 of 33

Limitations of Comparable<T>

  • Must have access to class T to make it implement the interface
  • There is only one kind of ordering on T -- whatever is built into compareTo()

29 of 33

Example interface: Comparator<T>

  • Define class outside T which overrides one method:

int compare(T o1, T o2)

30 of 33

Example interface: Comparator<T>

  • Define class outside T which overrides one method:

int compare(T o1, T o2)

    • Like compareTo(), but two arguments: returns negative number if o1 < o2, 0 if they're equal, and positive number otherwise

31 of 33

Example interface: Comparator<T>

  • Define class outside T which overrides one method:

int compare(T o1, T o2)

    • Like compareTo(), but two arguments: returns negative number if o1 < o2, 0 if they're equal, and positive number otherwise
  • Allows you to customize what T is sorted on: alphabetically, numerically, field 1 vs. field 2, etc.

32 of 33

Example interface: Comparator<T>

  • Define class outside T which overrides one method:

int compare(T o1, T o2)

    • Like compareTo(), but two arguments: returns negative number if o1 < o2, 0 if they're equal, and positive number otherwise
  • Allows you to customize what T is sorted on: alphabetically, numerically, field 1 vs. field 2, etc.
  • Pass comparator object as second argument to sort()

33 of 33

Comparator<T> example with sort()

class ByAlpha extends Comparator<String> {

public int compare(String o1, String o2) {

return o1.compareTo(o2);

}

}

class ByLength extends Comparator<String> {

public int compare(String o1, String o2) {

return (o1.length() - o2.length());

}

}