ObjectOriented Software Engineering CS 288 1 Interfaces Interfaces
Object-Oriented Software Engineering CS 288 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 add. Room(Room new. Room); void set. Room(Vector<Room> new. Rooms); Vector get. Rooms(); String[ ][ ] rooms. To. Array(); } 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, 2 nd example interface package building; import java. util. Vector; public interface House_interface { void set. Street. Name (); String get. String. Name (); void show. Street. Name (); void set. Occupant. Name (); String get. Occupant. Name (); void show. Occupant. Name (); void set. Street. Number(Double street. Number); Double get. Street. Number(); } 6
Interfaces in Java, 2 nd 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 h 1 = new House( ); House_interface hi 1 = (House_interface)h 1; Building_interface bi 1 = (Builging_interface)h 1; • 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 h 1 we can call get. Rooms with h 1. get. Rooms() even though get. Rooms 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 Rented. House 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 Rented. House */ public Rented. House(String street. Name, Double street. Number, String occupant. Name, int rent, String landlord) { super (street. Name, street. Number, occupant. Name); 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: Rented. House rh 1 = new Rented. House(); House h 1 = (House)rh 1; Hence any method that takes a House object as argument can be given a Rented. House object when it is cast as a House. 14
Inheritance, using super keyword public class Rented. House extends House { private int rent; private String landlord; private static String tax_occupant = " house empty"; private boolean tax_return = false; public Rented. House(String street. Name, Double street. Number, String occupant. Name, int rent, String landlord) { super(street. Name, street. Number, occupant. Name); this. rent = rent; Have overwritten method this. landlord = landlord; which hides it } public String get. Occupant. Name () { if (tax_return) { return tax_occupant; } else { return super. get. Occupant. Name(); } } } 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 get. Class() • public int hash. Code() • public String to. String() 16
Class get. Class example public static void main(String args[ ]) { /* Go and do some stuff */ House h 1 = new House ("Olderman St", 12. 0, "John Cumbersome") ; Class cl 1 = h 1. get. Class(); String house_class_str = cl 1. get. Canonical. Name(); String object_string = h 1. to. String(); } 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 h 1 = new House ("Olderman St", 12. 0, "John Cumbersome") ; House h 2 = new House ("Olderman St", 12. 0, "John Cumbersome") ; boolean tst = h 1. equals(h 2); } 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 get. Occupant. Name () { if (tax_return) { return tax_occupant; } else { return super. get. Occupant. Name(); } } 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 get. Rooms() { return rooms; } /* and so on for other implemented methods */ abstract void show. Street. Name (); abstract void show. Occupant. Name (); } 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
- Slides: 23