cs288_Lecture006

Download Report

Transcript cs288_Lecture006

Object-Oriented Software Engineering
CS288
1
Interfaces
•
•
•
•
Interfaces are contracts
Contracts between software groups
Defines how software interacts with other software
Should be able to write own code if you know what
other code computes, but without knowing how
other people’s code is written (in a perfect world)
• Presentation relies on material from
http://java.sun.com/docs/books/tutorial/index.html
2
Interfaces in Java
• Interface is a reference type,
– can’t have run time objects of that type until some other class
implements the interface
– can have method argument of that type
– can declare objects of that type
• Can contain only
– constants,
– method signatures,
– and nested types
• There are no method bodies or instance fields
• Can only be implemented by classes or extended by other
interfaces
• (Extensions are later in this presentation)
3
Interfaces in Java, example interface
package building;
import java.util.Vector;
public interface Building_interface {
void addRoom(Room newRoom);
void setRoom(Vector<Room> newRooms);
Vector getRooms();
String[ ][ ] roomsToArrayArray();
}
Note, still have same basic
Java sytax for importing other
classes, packages etc
Specify
method name
method arguments
return type
4
Interfaces in Java, example interface
package building;
import java.util.Vector;
public class Building implements Building_interface {
/* code goes here for all methods listed in
Building_interface */
/* class may also contain other methods not in interface */
}
5
Interfaces in Java, 2nd example interface
package building;
import java.util.Vector;
public interface House_interface {
void setStreetName ();
String getStringName ();
void showStreetName ();
void setOccupantName ();
String getOccupantName ();
void showOccupantName ();
void setStreetNumber(Double streetNumber);
Double getStreetNumber();
}
6
Interfaces in Java, 2nd example interface
package building;
public class House implements Building_interface, House_interface {
/* code goes here for all methods in all interfaces that
are listed
*/
/* as before can also include other methods */
}
7
Interfaces and Multiple Inheritance
• Interfaces are not part of the Java class hierarchy
• A class can inherit from exactly one class at most but it can implement
more than one interface
• Objects of class House for example can be cast as objects of type
House_interface
House h1 = new House( );
House_interface hi1 = (House_interface)h1;
Building_interface bi1 = (Builging_interface)h1;
• In other classes we can write methods that take a House_interface
object as an argument. We can then use a House object as a valid
argument by casting it to the relevant type as above.
8
Inheritance
• A Java class can be derived from other classes, thereby
inheriting fields and methods from those classes.
• Class derived from other class is called a subclass, or extended
class, or child class.
• Class the subclass is derived from is called a superclass, base
class or parent class.
• Every class has exactly one direct superclass
• In the absence of any explicit superclass, every class is
implicitly a subclass of Object.
• Except the Object class, which has no superclass
9
Inheritance
• To create a new class when there is already a class that
includes some of the code that you want, you can derive your
new class from the existing class.
•
In doing this, you can reuse the fields and methods of the
existing class without having to reinvent the wheel.
• Subclass inherits all public and protected fields, methods and
nested inner classes of superclass
• Does NOT inherit any constructors
• Superclass constructors can be invoked from a subclass
constructor
10
Inheritance, Example
• House class again, but this time we resuse the code from the
Building class
• public class House extends Building implements
Building_interface, House_interface {
/* only need to implement the
* House_interface methods now.
* All Building_interface methods
* inherited from Building class
*/
}
• Any House object has methods from Building class and
methods added in House class.
• Refer to inhereted methods in exactly same way as instance
methods.
E.g for House object h1 we can call getRooms with
h1. getRooms() even though getRooms is a Building method.
11
Building Example
Informal UML class diagram showing basic hierarchy in Building project
12
Calling superclass constructor in constructor method
package building;
public class RentedHouse extends House {
Have not declared the class
as implementing any interfaces
in this example
private int rent;
private String landlord;
/** Creates a new instance of RentedHouse */
public RentedHouse(String streetName, Double streetNumber, String
occupantName, int rent, String landlord) {
super (streetName, streetNumber, occupantName);
this.rent = rent;
this.landlord = landlord;
}
/* and so on */
}
Call to constructor method
of superclass: House
Must be on first line of
constructor method body
13
Inheritance
• If declare a field in the subclass with same name as one in the
superclass then superclass field is hidden by new field.
• Same comment as above goes for methods, overwritting in
subclass hides the ones in the superclass.
• Can declare new fields in subclass that are not in superclass.
• You can declare new methods and fields in the subclass that are
not in the superclass.
• Subclass does not inherit private members of its parent class,
but can still acces them with get methods if they were
implemented.
• Subclass objects can always be cast to have type of superclass:
RentedHouse rh1 = new RentedHouse();
House h1 = (House)rh1;
Hence any method that takes a House object as argument can
be given a RentedHouse object when it is cast as a House.
14
Inheritance, using super keyword
public class RentedHouse extends House {
private int rent;
private String landlord;
private static String tax_occupant = " house empty";
private boolean tax_return = false;
public RentedHouse(String streetName, Double streetNumber, String
occupantName, int rent, String landlord) {
super(streetName, streetNumber, occupantName);
this.rent = rent;
Have overwritten method
this.landlord = landlord;
which hides it
}
public String getOccupantName () {
if (tax_return) {
return tax_occupant;
} else {
return super.getOccupantName();
}
}
}
in superclass,
Can still call hidden
super class method
by using keyword
super as object identifier.
That then calls the current
object but as if it is of the
type for the superclass.
15
Object as a Superclass
• The Object class is the root of the class hierarchy
• Hence any method for the Object class is available to
any object of any other class (of course some of them
may have been overwritten).
• public boolean equals(Object obj)
• public final Class getClass()
• public int hashCode()
• public String toString()
16
Class getClass example
public static void main(String args[ ]) {
/* Go and do some stuff */
House h1 = new House ("Olderman St", 12.0, "John Cumbersome") ;
Class cl1 = h1.getClass();
String house_class_str = cl1.getCanonicalName();
String object_string = h1.toString();
}
17
Object equals method
• Compares two object references to test if they refer
to the same object.
• Usually needs to be overwritten to make sense.
public static void main(String args[ ]) {
/* Go and do some stuff */
House h1 = new House ("Olderman St", 12.0, "John Cumbersome") ;
House h2 = new House ("Olderman St", 12.0, "John Cumbersome") ;
boolean tst = h1.equals(h2);
}
Will return false
18
Writing Final Classes and Methods
• Can prevent a class being extended by declaring it final
• Similarly any individual method can be declared final to stop it
being overwritten in any subclass
public final String getOccupantName () {
if (tax_return) {
return tax_occupant;
} else {
return super.getOccupantName();
}
}
19
Abstract Classes
• Sometimes we want a class that is half way between an
interface and a proper class
• It specifies some methods to be implemented in
subclasses
• It gives the implementation for some methods as well
• An abstract class does just this job
• Abstract class is extended to a concrete class rather than
be implemented
• A subclass of an abstract class must then implement the
abstract methods whilst it can inherit those methods that
have already been implemented
20
Abstract class example, Building class
can still have an abstract class
implement an interface if we want
public abstract class Building implements Building_interface {
public Vector getRooms() {
return rooms;
}
/* and so on for other implemented methods */
abstract void showStreetName ();
abstract void showOccupantName ();
}
for abstract methods just add signature
with abstract keyword
These MUST be implemented in subclass
21
Abstract class
• Where an abstract class has every method declared abstract
and no instance fields then best to change it to an interface
instead
• That way any other class can still be a subclass and implement
this interface as well.
22
23