9 Improving structure with inheritance BK Chap 10
9 Improving structure with inheritance BK Chap. 10
Main concepts to be covered • • Inheritance Subtyping Substitution Polymorphic variables Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 2
The Network example • A small, prototype social network. • Supports a news feed with posts. • Stores text posts and photo posts. – Message. Post: multi-line text message. – Photo. Post: photo and caption. • Allows operations on the posts: – E. g. , search, display and remove. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 3
Network objects Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 4
Network classes Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 5
Network object model Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 6
Class diagram Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 7
Message. Post source code public class Message. Post { private String username; private String message; private long timestamp; private int likes; private Array. List<String> comments; public Message. Post(String author, String text) { username = author; message = text; timestamp = System. current. Time. Millis(); likes = 0; comments = new Array. List<String>(); } Just an outline public void add. Comment(String text). . . public void like(). . . public void display(). . . } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 8
Photo. Post source code public class Photo. Post { private String username; private String filename; private String caption; private long timestamp; private int likes; private Array. List<String> comments; public Photo. Post(String author, String filename, String caption) { username = author; this. filename = filename; this. caption = caption; timestamp = System. current. Time. Millis(); likes = 0; comments = new Array. List<String>(); } Just an outline public void add. Comment(String text). . . public void like() … public void display() …. . . } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 9
News. Feed public class News. Feed { private Array. List<Message. Post> messages; private Array. List<Photo. Post> photos; . . . public void show() { for(Message. Post message : messages) { message. display(); System. out. println(); // empty line between posts } for(Photo. Post photo : photos) { photo. display(); System. out. println(); // empty line between posts } } } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 10
Critique of Network • Code duplication: – Message. Post and Photo. Post classes very similar (large parts are identical) – makes maintenance difficult/more work – introduces danger of bugs through incorrect maintenance • Code duplication in News. Feed class as well. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 11
Using inheritance Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 12
Using inheritance • define one superclass : Post • define subclasses for Message. Post and Photo. Post • the superclass defines common attributes (via fields) • the subclasses inherit the superclass attributes • the subclasses add other attributes Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 13
Inheritance hierarchies Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 14
Inheritance in Java public class Post {. . . } no change here public class Photo. Post extends Post {. . . } public class Message. Post extends Post {. . . } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 15
Superclass public class Post { private String username; private long timestamp; private int likes; private Array. List<String> comments; // constructor and methods omitted. } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 16
Subclasses public class Message. Post extends Post { private String message; // constructor and methods omitted. } public class Photo. Post extends Post { private String filename; private String caption; // constructor and methods omitted. } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 17
Inheritance and public class Post { constructors private String username; private long timestamp; private int likes; private Array. List<String> comments; /** * Initialise the fields of the post. */ public Post(String author) { username = author; timestamp = System. current. Time. Millis(); likes = 0; comments = new Array. List<String>(); } // methods omitted } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 18
Inheritance and constructors public class Message. Post extends Post { private String message; /** * Constructor for objects of class Message. Post */ public Message. Post(String author, String text) { super(author); message = text; } // methods omitted } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 19
Superclass constructor call • Subclass constructors must always contain a 'super' call. • If none is written, the compiler inserts one (without parameters) – works only, if the superclass has a constructor without parameters • Must be the first statement in the subclass constructor. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 20
Adding more item types Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 21
Deeper hierarchies Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 22
Review (so far) Inheritance (so far) helps with: • Avoiding code duplication • Code reuse • Easier maintenance • Extendibility Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 23
public class News. Feed { private Array. List<Post> posts; /** * Construct an empty news feed. */ public News. Feed() { posts = new Array. List<Post>(); } /** * Add a post to the news feed. */ public void add. Post(Post post) { posts. add(post); }. . . Revised News. Feed source code avoids code duplication in the client class! } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 24
New News. Feed source code /** * Show the news feed. Currently: print the * news feed details to the terminal. * (Later: display in a web browser. ) */ public void show() { for(Post post : posts) { post. display(); System. out. println(); // Empty line. . . } } Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 25
Subtyping First, we had: public void add. Message. Post( Message. Post message) public void add. Photo. Post( Photo. Post photo) Now, we have: public void add. Post(Post post) We call this method with: Photo. Post my. Photo = new Photo. Post(. . . ); feed. add. Post(my. Photo); Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 26
Subclasses and subtyping • Classes define types. • Subclasses define subtypes. • Objects of subclasses can be used where objects of supertypes are required. (This is called substitution. ) Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 27
Types are sets of objects • The type T defined by the class C is the set of all objects of class C and all objects of all subclasses of C. Post Message. Post Photo. Post A Message. Post is a Post A Photo. Post is a Post Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 28
Polymorphic variables • Reference variables in Java are polymorphic. - They can hold objects of more than one type. • They can hold objects of the declared type, or of subtypes of the declared type. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 29
Polymorphic variables cont. Post p; Message. Post mp; Photo. Post pp; OK! p : Message. Post : Post username timestamp likes comments message username timestamp likes comments OK! ! O N mp Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 : Photo. Post username timestamp likes comments filename caption ! O N OK! pp Förel. 9 30
Subtyping and assignment subclass objects may be assigned to superclass variables Vehicle v 1 = new Vehicle(); Vehicle v 2 = new Car(); Vehicle v 3 = new Bicycle(); Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 31
The instanceof operator A Message. Post is a Post A Photo. Post is a Post p = new p instanceof Post(…); Post Message. Post Photo. Post true false p p = new Message. Post(…); instanceof Post instanceof Message. Post instanceof Photo. Post true false p p = new Photo. Post(…); instanceof Post instanceof Message. Post instanceof Photo. Post true false true Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 32
Subtyping and visibility Post p = new Post(…); p. like(); p. unlike(); p. add. Comment(); p. get. Time. Stamp(); p. display(); p = new Message. Post(…); p. like(); p. unlike(); p. add. Comment(); p. get. Time. Stamp(); p. display(); p. get. Text(); // Unknown in Post! … fields omitted like unlike add. Comment get. Time. Stamp display Message. Post … fields omitted p = new Photo. Post(…); p. like(); p. unlike(); p. add. Comment(); p. get. Time. Stamp(); p. display(); p. get. Image. File(); // Unknown in Post! p. get. Caption(); // Unknown in Post! Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 get. Text Photo. Post … fields omitted get. Image. File get. Caption The visibility of class attributes is determined by the declared type of p, regardless of which object p refers to. Förel. 9 33
Subtyping and visibility cont. Message. Post p = new Message. Post(…); p. like(); p. unlike(); p. add. Comment(); Inherited from Post p. get. Time. Stamp(); p. display(); p. get. Text(); p. get. Image. File(); // Unknown in Message. Post p. get. Caption(); // Unknown in Message. Post Photo. Post p = new Photo. Post(…); p. like(); p. unlike(); p. add. Comment(); Inherited from Post p. get. Time. Stamp(); p. display(); p. get. Image. File(); p. get. Caption(); p. get. Text(); // Unknown in Photo. Post Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Post … fields omitted like unlike add. Comment get. Time. Stamp display Message. Post … fields omitted get. Text Photo. Post … fields omitted get. Image. File get. Caption Förel. 9 34
Subtyping and parameter passing public class News. Feed { public void add. Post(Post post) {. . . } } subclass objects may be used as actual parameters for the superclass Photo. Post photo = new Photo. Post(. . . ); Message. Post message = new Message. Post(. . . ); feed. add. Post(photo); feed. add. Post(message); Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 35
Object diagram Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 36
Class diagram Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 37
Casting • We can assign subtype to supertype … • … but we cannot assign supertype to subtype! Vehicle Car c = v = c; c = v; new Car(); // correct // compile-time error! • Casting fixes this: c = (Car) v; (only ok if the vehicle really is a Car!) Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 38
Casting • • An object type in parentheses. Used to overcome 'type loss'. The object is not changed in any way. A runtime check is made to ensure the object really is of that type: – Class. Cast. Exception if it isn't! • Use it sparingly. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 39
The Object class All classes inherit from Object. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 40
Polymorphic collections • All collections are polymorphic. • The elements could simply be of type Object. public void add(Object element) public Object get(int index) • Usually avoided by using a type parameter with the collection. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 41
Polymorphic collections • A type parameter limits the degree of polymorphism: Array. List<Post> • Collection methods are then typed. • Without a type parameter, Array. List<Object> is implied. • Likely to get an “unchecked or unsafe operations” warning. • More likely to have to use casts. Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 42
Polymorphic collections (3) Ex. Whatch out for type cast errors! Array. List box = new Array. List(); Integer banana = new Integer(37); box. add(”apple”); box. add(banana); Type cast Object to String fruit = (String)box. get(0); fruit = (String)box. get(1); Oops! The next element is an Integer Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 43
Polymorphic collections (4) • Polymorphic collections means weaker type checking. • Type checking that could be done at compile time is done at runtime. • Use generic collections instead! Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 44
Review • Inheritance allows the definition of classes as extensions of other classes. • Inheritance – – avoids code duplication allows code reuse simplifies the code simplifies maintenance and extension • Variables can hold subtype objects. • Subtypes can be used wherever supertype objects are expected (substitution). Objektorienterad programmering, DAT 050, DAI 2, 19/20, lp 1 Förel. 9 45
- Slides: 45