object-oriented software engineering

23
1 Object-Oriented Software Engineering CS288

Upload: vila

Post on 05-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Object-Oriented Software Engineering. CS288. Interfaces. Interfaces are contracts Contracts between software groups Defines how software interacts with other software - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object-Oriented Software Engineering

1

Object-Oriented Software Engineering

CS288

Page 2: Object-Oriented Software Engineering

2

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 fromhttp://java.sun.com/docs/books/tutorial/index.html

Page 3: Object-Oriented Software Engineering

3

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)

Page 4: Object-Oriented Software Engineering

4

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(); }

Specify method namemethod argumentsreturn type

Note, still have same basicJava sytax for importing otherclasses, packages etc

Page 5: Object-Oriented Software Engineering

5

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 */

}

Page 6: Object-Oriented Software Engineering

6

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();}

Page 7: Object-Oriented Software Engineering

7

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 */

}

Page 8: Object-Oriented Software Engineering

8

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.

Page 9: Object-Oriented Software Engineering

9

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

Page 10: Object-Oriented Software Engineering

10

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

Page 11: Object-Oriented Software Engineering

11

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 withh1. getRooms() even though getRooms is a Building method.

Page 12: Object-Oriented Software Engineering

12

Building Example

Informal UML class diagram showing basic hierarchy in Building project

Page 13: Object-Oriented Software Engineering

13

Calling superclass constructor in constructor method

package building;

public class RentedHouse extends House { 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: HouseMust be on first line ofconstructor method body

Have not declared the classas implementing any interfacesin this example

Page 14: Object-Oriented Software Engineering

14

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.

Page 15: Object-Oriented Software Engineering

15

Inheritance, using super keywordpublic 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; this.landlord = landlord; } public String getOccupantName () { if (tax_return) { return tax_occupant; } else { return super.getOccupantName(); } } }

Have overwritten method in superclass,which hides it

Can still call hiddensuper class methodby using keywordsuper as object identifier.That then calls the currentobject but as if it is of the type for the superclass.

Page 16: Object-Oriented Software Engineering

16

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()

Page 17: Object-Oriented Software Engineering

17

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(); }

Page 18: Object-Oriented Software Engineering

18

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

Page 19: Object-Oriented Software Engineering

19

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(); } }

Page 20: Object-Oriented Software Engineering

20

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

Page 21: Object-Oriented Software Engineering

21

Abstract class example, Building class

public abstract class Building implements Building_interface {

can still have an abstract class implement an interface if we want

public Vector getRooms() { return rooms; } /* and so on for other implemented methods */

abstract void showStreetName (); abstract void showOccupantName ();}

for abstract methods just add signaturewith abstract keywordThese MUST be implemented in subclass

Page 22: Object-Oriented Software Engineering

22

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.

Page 23: Object-Oriented Software Engineering

23