CSCU9T4: Object Modelling, principles of OO design and implementation CSCU9T4 Spring 2016 1 What is Inheritance? Why use Inheritance? Discussion CSCU9T4 Spring 2016 2
Inheritance In inheritance, a subclass "extends" the definition of its superclass It implicitly includes the attributes and operations from the superclass, and may add attributes and operations and may redefine the implementations of operations For example, inheritance is shown in the following, where classes Book and Journal "inherit from" Publication: Note: UML notation. Arrowheads can be closed: inheritance open: reference (via an attribute) CSCU9T4 Spring 2016 3 Inheritance - extends public class Publication{... public class Book extends Publication {... public class Journal extends Publication {... We can have an inheritance hierarchy If, say, Publication is itself a subclass of an even higher-level class Or, say, Book has subclasses CSCU9T4 Spring 2016 4
public abstract class Publication { protected int catnum; protected String title; // operations public class Book extends Publication { private String author; // operations public class Journal extends Publication { private int volnum; // operations CSCU9T4 Spring 2016 5 Inheritance & Attributes Although we want to hide the attributes of a superclass from ordinary clients, we usually want to make them visible in the subclass This is not automatic with private attributes Hence, we there are three levels of visibility: public, private and protected. public : visible to clients private : visible only within the class protected : visible only within the class and its subclasses So that the method bodies in Book and Journal can refer to title and catnum: protected int catnum; protected String title; CSCU9T4 Spring 2016 6
Object Modelling Art Collection Revisit the art collection example: Consider a system to keep track of the art collection of the university. This might have a number of purposes: to allow the collection to be browsed to allow other galleries to borrow items What are the main entities in the system? How are they related? CSCU9T4 Spring 2016 7 Executing program and GUI The JVM launches the program by calling the main method This typically creates an instance of the Boundary class Boundary app = new Boundary(); Typically the Boundary constructor then builds a GUI (JFrame) The interactions between the user and the program are represented in Boundary as a set of operations. What are these for the Art Collection? In a GUI, each operation can be implemented by a button and the parameters by information in text fields. What are these for the Art Collection? CSCU9T4 Spring 2016 8
What s the highest object in the hierarchy? (How do we use a gallery/catalog/?) What operations are provided to the user (via actionperformed)? Trace the program: what happens when a button is pressed? what happens when we create a collection object? (what operation is called, what information is needed?) what happens when we create an artwork object? (what operation is called, what information is needed?) CSCU9T4 Spring 2016 9 Inheritance & Operations CSCU9T4 Spring 2016 10
public abstract class Publication { protected int catnum; protected String title; public String gettitle() {... public int getcatnum() {... public abstract void borrow(member m); public void return() {... public class Book extends Publication { private String author; public String getauthor() {... public void borrow(member m) {... public class Journal extends Publication { private int volnum; public int getvol() {... public void borrow(member m) {... CSCU9T4 Spring 2016 11 Inheritance & Operations Note that the operation borrow is given in all three classes Publication, Book and Journal That shows that it is defined in Publication... And the definition is overridden in the Book and Journal subclasses This is useful if we wish to indicate: That all Publications, whether they are a book or a journal, have a public borrow operation But that the actual details for borrowing a Book and borrowing a Journal are different so the two subclasses must have separately defined methods We also need to decide whether we need an actual implementation (method body) of borrow in Publication There does not need to be, and in this example there is not indicated by the keyword abstract (See discussion of "abstract methods" later on) CSCU9T4 Spring 2016 12
Instantiation of subclasses To be completely clear: When a subclass is instantiated using new: Enough storage is allocated for all the attributes of the subclass and its one (or more) superclasses Effectively, all the subclass's operations are included, plus all the non-overridden operations from the superclasses except the constructors A subclass constructor may call its superclass's constructor using super( );» (This is Java; details may vary slightly for other OO languages) Therefore: An instance of a subclass has all the properties expected of an instance of a superclass For example: a Book has title, gettitle, catnum, return, borrow, etc CSCU9T4 Spring 2016 13 A consequence of the previous slide, and a feature of inheritance in object-oriented systems is that: Anywhere a reference to a superclass object is expected, we can use a reference to a subclass object instead because we can guarantee that it has all the capabilities Hence, a reference to a Publication object could refer to a Book object Consider some examples: are these valid? What do they do? Publication pub = new Publication("Smith"); Book pub = new Book("Smith"); Publication pub = new Book( Smith"); Book pub = new Publication("Smith"); CSCU9T4 Spring 2016 14
Further: When we call an operation through a reference, the actual method used depends on the actual object referred to (Again, this is Java, and details may vary in other OO languages) For example, in Java, after the declaration: Publication pub = new Book( Smith"); What happens if we call the borrow operation: pub.borrow(...); What if we added pub = new Journal( LNCS"); Now what happens if we call pub.borrow(); CSCU9T4 Spring 2016 15 Overriding is not Overloading Any class may have more than one method with the same name, but we have overloading when: The formal parameters lists are distinguishable This allows the compiler/jvm to determine which actual method is being called. E.g. JButton b1 = new JButton(); JButton b2 = new JButton("Press me"); If Book has, say: public void setdetails(string t) {... public void setdetails(string t, String a) {... then book1.setdetails( The Oxford English Dictionary"); book1.setdetails( The Wonderbox", Roman Krznaric"); CSCU9T4 Spring 2016 16
This is polymorphism and dynamic binding Try reading: http://docs.oracle.com/javase/ tutorial/java/iandi/polymorphism.html CSCU9T4 Spring 2016 17 Art Collection and Inheritance Revisit the art collection example: Are there operations occurring in superclasses and subclasses here? Which operations are the same in all? And which operations are different? CSCU9T4 Spring 2016 18
Why use Inheritance? Discussion CSCU9T4 Spring 2016 19 End of lecture CSCU9T4 Spring 2016 20