Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be...

82

Transcript of Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be...

Page 1: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Lecture 6

Abstract Classes, Generics and Interfaces...

Summer 2013

M. Jason HinekCarleton University

Page 2: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Day 6

· a quick look back (day 5)

· assignments

· abstract classes· inheritance revisited· (plymorphism)

· generics· ArrayList class

· interfaces· Comparable class

· a quick look ahead (day 7)

2 / 24

Page 3: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Last time...

The material in Day 5 touched on

· static

· instance vs static attributes· instance vs static methods

· super() and this()

· can only be used as the �rst line of constructor

· super

· allows access to parent class overridden methods

· final

· class, methods and attributes

· abstract

· classes and methods

3 / 24

Page 4: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Assignments

· assignment 5

· assignment 6

4 / 24

Page 5: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Methods

· abstract methods have no body (de�nition)· public abstract String getName();

· cannot be final

· method will be overridden in a child class

· forces class to also be abstract

5 / 24

Page 6: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

· abstract classes cannot be instantiated· no objects of the class can exist

· cannot be final

· no restrictions on what can be in an abstract class· attributes, methods, constructors· does not need to have abstract methods

· intention is for a descendant class to be concrete

6 / 24

Page 7: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

7 / 24

Page 8: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

7 / 24

Page 9: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

GameCharacters

↑ ↑User Game

↑ ↑Friend Foe

7 / 24

Page 10: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

GameCharacters↑ ↑

User Game

↑ ↑Friend Foe

7 / 24

Page 11: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

GameCharacters↑ ↑

User Game↑ ↑

Friend Foe

7 / 24

Page 12: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

abstract GameCharacters

↑ ↑User Game

↑ ↑Friend Foe

7 / 24

Page 13: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

abstract GameCharacters

↑ ↑User abstract Game

↑ ↑Friend Foe

7 / 24

Page 14: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes

why use abstract classes?

· why use inheritance?

abstract GameCharacters

↑ ↑User abstract Game

↑ ↑Friend Foe

· provides interface between implementation and users of code

· helps modularize code

7 / 24

Page 15: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding

· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding

· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 16: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding

· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding

· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 17: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden

· same method signature (name and argument list) and return type*

· method hiding

· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 18: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding

· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 19: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding

· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 20: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden

· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 21: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 22: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)

· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 23: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)· any attribute in the parent class with the same name is hidden

· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 24: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)· any attribute in the parent class with the same name is hidden· type of attribute does not matter

· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 25: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 26: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour

· tutorial 3 will look at this

8 / 24

Page 27: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Overriding and Hiding

inheritance can get tricky...

· method overriding· instance methods are overridden· same method signature (name and argument list) and return type*

· method hiding· static methods are hidden· same method signature (name and argument list) and return type*

· attribute hiding (�eld hiding)· any attribute in the parent class with the same name is hidden· type of attribute does not matter· Avoid attribute hiding!

· method overriding/hiding leads to di�erent behaviour· tutorial 3 will look at this

8 / 24

Page 28: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

let's take a break...for 5 minutes

9 / 24

Page 29: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operations

an ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 30: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values

create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 31: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values

create an empty list

(need not be unique)

ask if the list is empty or notask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 32: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique)

ask if the list is empty or notask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 33: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 34: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list is

add an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 35: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the list

remove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 36: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the list

ask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 37: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 38: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

consider the List ADT

List ADTdata operationsan ordered collection of values create an empty list(need not be unique) ask if the list is empty or not

ask what the size of the list isadd an item to the listremove an item from the listask which item is at a given position

· an ordered collection with arbitrary access to items in the collection

10 / 24

Page 39: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class implements the List ADT in Java

· the values in an ArrayList are Objects by default

· this is annoying when using an ArrayList

· ArrayList uses generics

· this allows us to specify what kind of list we want to havewithout requiring a new class for each di�erent kind

11 / 24

Page 40: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class implements the List ADT in Java

· the values in an ArrayList are Objects by default

· this is annoying when using an ArrayList

· ArrayList uses generics

· this allows us to specify what kind of list we want to havewithout requiring a new class for each di�erent kind

11 / 24

Page 41: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class implements the List ADT in Java

· the values in an ArrayList are Objects by default

· this is annoying when using an ArrayList

· ArrayList uses generics

· this allows us to specify what kind of list we want to havewithout requiring a new class for each di�erent kind

11 / 24

Page 42: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class implements the List ADT in Java

· the values in an ArrayList are Objects by default

· this is annoying when using an ArrayList

· ArrayList uses generics

· this allows us to specify what kind of list we want to havewithout requiring a new class for each di�erent kind

11 / 24

Page 43: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class implements the List ADT in Java

· the values in an ArrayList are Objects by default

· this is annoying when using an ArrayList

· ArrayList uses generics

· this allows us to specify what kind of list we want to havewithout requiring a new class for each di�erent kind

11 / 24

Page 44: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

the ArrayList class uses generics

public class ArrayList<E> ...

public boolean add(E e)

// Appends specified element to the end of this list

public void add(int index, E element)

// Inserts element to specified position in list

public E get(int index)

// Returns the element at the specified position

12 / 24

Page 45: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

ArrayList<String> list = new ArrayList<String>();

public class ArrayList<String> ...

public boolean add(String e)

// Appends specified element to the end of this list

public void add(int index, String element)

// Inserts element to specified position in list

public String get(int index)

// Returns the element at the specified position

13 / 24

Page 46: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

ArrayList<House> list = new ArrayList<House>();

public class ArrayList<House> ...

public boolean add(House e)

// Appends specified element to the end of this list

public void add(int index, House element)

// Inserts element to specified position in list

public House get(int index)

// Returns the element at the specified position

14 / 24

Page 47: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

ArrayList<Bunny> list = new ArrayList<Bunny>();

public class ArrayList<Bunny> ...

public boolean add(Bunny e)

// Appends specified element to the end of this list

public void add(int index, Bunny element)

// Inserts element to specified position in list

public Bunny get(int index)

// Returns the element at the specified position

15 / 24

Page 48: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

think of E as input paramater to class

public class ArrayList<E> ...

public boolean add(E e)

// Appends specified element to the end of this list

public void add(int index, E element)

// Inserts element to specified position in list

public E get(int index)

// Returns the element at the specified position

16 / 24

Page 49: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

generics in Java let's us use types as parameters in classes, methods(and interfaces)

· allows for generic algorithms

· eliminates casts

· provides stronger compile time type checks

· only work with objects! (no primitive data types)

17 / 24

Page 50: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

generics in Java let's us use types as parameters in classes, methods(and interfaces)

· allows for generic algorithms

· eliminates casts

· provides stronger compile time type checks

· only work with objects! (no primitive data types)

17 / 24

Page 51: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

generics in Java let's us use types as parameters in classes, methods(and interfaces)

· allows for generic algorithms

· eliminates casts

· provides stronger compile time type checks

· only work with objects! (no primitive data types)

17 / 24

Page 52: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

generics in Java let's us use types as parameters in classes, methods(and interfaces)

· allows for generic algorithms

· eliminates casts

· provides stronger compile time type checks

· only work with objects! (no primitive data types)

17 / 24

Page 53: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

generics in Java let's us use types as parameters in classes, methods(and interfaces)

· allows for generic algorithms

· eliminates casts

· provides stronger compile time type checks

· only work with objects! (no primitive data types)

17 / 24

Page 54: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

· generic classes· public class ClassName<T1, T2, ... TN>{...}

· generic methods· public static <T,K> ReturnType methodName(...){...}

· examples...

18 / 24

Page 55: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Generics

· generic classes· public class ClassName<T1, T2, ... TN>{...}

· generic methods· public static <T,K> ReturnType methodName(...){...}

· examples...

18 / 24

Page 56: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

let's take a break...for 7 minutes

19 / 24

Page 57: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class

· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 58: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class

· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 59: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 60: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 61: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 62: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 63: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code

· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 64: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 65: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 66: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Abstract Classes

· related classes extend the same abstract class· similar behaviour is implemented in the abstract class itself· di�erent behaviour is implemented in overridden abstract methods

· you can only extend one single class

· all classes that extend a given abstract class are related

· abstract class provides a contract between code and users of code

Java Interfaces

· provide a contract between code and users of code· typically speci�es how di�erent code interacts with each other

· a class can implement any number of interfaces

· classes that implement a given interface can be very unrelated

20 / 24

Page 67: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)

· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes

· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 68: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)

· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes

· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 69: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)

· implicitly abstract (you do not write this)

· contain constant attributes

· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 70: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes

· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 71: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes

· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 72: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 73: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 74: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type

· cannot be instantiated (like an abstract class)

21 / 24

Page 75: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Abstract Classes and Interfaces

Java Interfaces

· contain method declarations (without de�nition)· are public by default (you can omit this)· implicitly abstract (you do not write this)

· contain constant attributes· are public static final by default (can omit this)

· can extend any number of other interfaces

· is a reference type· cannot be instantiated (like an abstract class)

21 / 24

Page 76: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

consider the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

// returns -1 if this is "less than" o

// returns 0 if this is "equal to" o

// returns +1 if this is "greater than" o

}

· declared like a class, using interface instead

· has a single method called compareTo()

· by default all methods are public abstract

· uses generics· avoids the messiness we saw with Object's equals()

22 / 24

Page 77: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· class that implements the Comparable interface either

· must de�ne the method compareTo, or· must be abstract

· public class SomeClass implements Comparable{ ...

public interface SomeInteerface extends Comparable{...

23 / 24

Page 78: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· class that implements the Comparable interface either

· must de�ne the method compareTo, or· must be abstract

· public class SomeClass implements Comparable{ ...

public interface SomeInteerface extends Comparable{...

23 / 24

Page 79: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· class that implements the Comparable interface either· must de�ne the method compareTo, or

· must be abstract

· public class SomeClass implements Comparable{ ...

public interface SomeInteerface extends Comparable{...

23 / 24

Page 80: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· class that implements the Comparable interface either· must de�ne the method compareTo, or· must be abstract

· public class SomeClass implements Comparable{ ...

public interface SomeInteerface extends Comparable{...

23 / 24

Page 81: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· class that implements the Comparable interface either· must de�ne the method compareTo, or· must be abstract

· public class SomeClass implements Comparable{ ...

public interface SomeInteerface extends Comparable{...

23 / 24

Page 82: Abstract Classes, Generics and Interfaces€¦ · Abstract Classes • abstract classes cannot be instantiated • no objects of the class can exist • cannot be final • no restrictions

Interfaces

using the Comparable interface

public interface Comparable<T>{

int compareTo(T o);

}

· let's see an example...

24 / 24