Documente Academic
Documente Profesional
Documente Cultură
COM6050 subclasses
Java and UML for Programmers Towards the top of a hierarchy classes are often more of a
basis for derived classes rather than a class with specific
Lecture 7: Interfaces and Abstract instances
Person
Steve Renals
http://www.dcs.shef.ac.uk/ sjr/com6050
Employee Student
24.10.2002
COM6050 / Lecture 7 – p.1/21 Why bother with the person superclass? COM6050 / Lecture 7 – p.3/21
Interfaces Introducing a person superclass lets us factor out the
Documentation using Javadoc
abstract class Person . . .
public abstract String getDescription();
implementation is not necessary does it
Any class with one or more abstract methods must be A class can implement one or more interfaces. Objects of
declared abstract these classes can then be used anytime something
Abstract classes can also have concrete data and methods conforming to the interface is required
- eg, getName can be implemented in Person Regard an interface as a set of requirements for a class
Think of abstract methods as placeholders to implemented The idea of separating interface from implementation is a
in concrete subclasses powerful one: it enables classes to be constructed that can
A class can be declared abstract even if it has no abstract operate on objects of any type, so long as they implement
methods the appropriate interface
// Assume Employee and Student extend abstract class Person if the objects belong to a class that implements the
Person[ ] people = new Person[2];
Comparable interface
people[0] = new Employee(. . .)
people[1] = new Student(. . .) Employee staff[ ] = new Employee[10];
... ...
for(int i = 0; i people.length; ++i)
Arrays.sort(staff);
// p.getDescription() is never undefined since all instances
// must be of a concrete class The Comparable interface:
+ people[i].getDescription()); public interface Comparable
int compareTo(Object other);
If the abstract method was not declared in Person, then could
compareTo method
COM6050 / Lecture 7 – p.6/21 COM6050 / Lecture 7 – p.8/21
Interfaces Properties of Interfaces
Interfaces can have zero or more methods, and can also Interfaces are not classes, and cannot be instantiated:
define constants x = new Comparable(); // ERROR!
Interfaces never have instance fields You can declare interface variables:
Interfaces never implement methods
Comparable x; // OK
An interface is a promise, or a contract: a class
x = new Employee(. . .); // OK if Employee implements Comparable
implementing an interface must implement the methods
declared in the interface Can use instanceof to check if an object implements an
To declare that a class implements an interface, use the interface:
implements keyword: if (x instanceof Comparable) . . .
class Employee implements Comparable Classes can implement multiple interfaces, eg:
Interfaces are required due to strong typing: when making public class Employee implements Comparable, Cloneable ...
a method call the compiler must know the method exists.
COM6050 / Lecture 7 – p.9/21 COM6050 / Lecture 7 – p.11/21
public class Employee implements Comparable public abstract class Comparable
... public abstract int compareTo(Object obj);
Employee e = (Employee) obj; public class Employee extends Comparable
...
// can’t return (payRate - e.payRate) since they are floating point public int compareTo(Object obj) ...
if(payRate e.payRate) return 1;
Problem: a class can only extend a single class, so this is not allowed:
public class Employee extends Comparable, Person
return 0;
But a class can extend a base class and implement one or more
interfaces: public class Employee extends Person implements Comparable
Copying Cloning
Employee
Employee defined with multiple implementations
original
copy
original a subclass of A
copy
A class C can implement an interface B, by implementing
Employee
interface: the type hierarchy is not changed
Employee orig = new Employee("Bill Gates", 100000);
Interfaces are ideal for mixins: types (such as Comparable)
Employee copy = orig;
copy.increasePayRate(10000); // increases orig too! that a class may implement in addition to its primary type
With interfaces non-hierarchical type frameworks are
COM6050 / Lecture 7 – p.13/21
possible COM6050 / Lecture 7 – p.15/21
But clone is protected method, so cannot simply call it
Public class
Object.clone() is protected because it copies objects field
Public interface
by field: OK for primitive types, but only copies references
Public or protected method
for object types. This is a problem for objects that contain
references to mutable objects. Public or protected variable or constant
Solution: (1) redefine clone as a public method; and (2) Thus you should supply a comment for each of these features
implement Cloneable interface
Redefining clone may involve calling clone on all object
instance fields (providing they are also Cloneable)
Cloneable is a “tagging” interface since it has no methods;
but it does allow the use of instanceof COM6050 / Lecture 7 – p.14/21 COM6050 / Lecture 7 – p.16/21
Javadoc Comments Javadoc: Method Comments
A comment is placed immediately above the feature it
describes /**
* Reads a fixed length string from an output stream
A documentation comment starts with /** and ends with */ *
* @param size length of string to read (may be terminated early by 0)
Each documentation comment starts with free-form text
* @param in code DataInput /code stream
followed by tags
* @return string read from stream
The first sentence of the free-form text should be a * @exception IOException if an error occurs
summary statement. This will be automatically extracted by */
public static String readFixedLengthString(int size, DataInput in)
javadoc
throws IOException
You can use HTML modifiers such as i . . . /i ,
. . .
b . . . /b , etc. Don’t use heading h1 or rule hr .
(NB @throws and @exception do the same thing)
A tag starts with @ such as @author and @param
* querying the document index ( code getRelevantDocumentsAnd /code and javadoc -d JavaDoc packageName
*
* @author a href=“mailto:sjr@dcs.shef.ac.uk“ Steve Renals /a
*/