abstract classes, generics and interfaces€¦ · abstract classes • abstract classes cannot be...
TRANSCRIPT
Lecture 6
Abstract Classes, Generics and Interfaces...
Summer 2013
M. Jason HinekCarleton University
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
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
Assignments
· assignment 5
· assignment 6
4 / 24
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
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
Abstract Classes
why use abstract classes?
· why use inheritance?
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
GameCharacters
↑ ↑User Game
↑ ↑Friend Foe
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
GameCharacters↑ ↑
User Game
↑ ↑Friend Foe
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
GameCharacters↑ ↑
User Game↑ ↑
Friend Foe
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
abstract GameCharacters
↑ ↑User Game
↑ ↑Friend Foe
7 / 24
Abstract Classes
why use abstract classes?
· why use inheritance?
abstract GameCharacters
↑ ↑User abstract Game
↑ ↑Friend Foe
7 / 24
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
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
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
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
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
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
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
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
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
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
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
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
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
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
let's take a break...for 5 minutes
9 / 24
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Generics
· generic classes· public class ClassName<T1, T2, ... TN>{...}
· generic methods· public static <T,K> ReturnType methodName(...){...}
· examples...
18 / 24
Generics
· generic classes· public class ClassName<T1, T2, ... TN>{...}
· generic methods· public static <T,K> ReturnType methodName(...){...}
· examples...
18 / 24
let's take a break...for 7 minutes
19 / 24
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Interfaces
using the Comparable interface
public interface Comparable<T>{
int compareTo(T o);
}
· let's see an example...
24 / 24