9 Improving structure with inheritance

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Inheritance in collections Week Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution.
CS1054: Lecture 18 - Inheritance. The DoME example "Database of Multimedia Entertainment" stores details about CDs and videos –CD: title, artist, # tracks,
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Improving structure with inheritance (Chapters 8 and 9)
Improving structure with inheritance
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
Inheritance Chapter 8.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
More about inheritance Exploring polymorphism 5.0.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Object Oriented Software Development
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
More about inheritance Exploring polymorphism 5.0.
M1G Introduction to Programming 2 5. Completing the program.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Improving structure with inheritance 3.0. The media project stores details about CDs and DVDs –CD: title, artist, number of tracks, playing time, got-it,
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
Java Unit 11: Inheritance I
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
More about inheritance
ADT’s, Collections/Generics and Iterators
10 More about inheritance
More about inheritance
Week 4 Object-Oriented Programming (1): Inheritance
COS 260 DAY 19 Tony Gauvin.
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Topic 6 Generic Data Structures
Understanding Inheritance
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
COS 260 DAY 19 Tony Gauvin.
Lecture 19 - Inheritance (Contd).
Extending Classes.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
MSIS 670 Object-Oriented Software Engineering
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Objects First with Java
Java Programming with BlueJ Objectives
Java Programming Language
Comp1202: Inheritance I Super and Sub-classes.
Review of Previous Lesson
Review: libraries and packages
Chapter 19 Generics.
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

9 Improving structure with inheritance Objektorienterad programmering DAI2, förel. 9 9 Improving structure with inheritance BK Chap. 10 DAT050, DAI2, 17/18, lp 2

Main concepts to be covered Objektorienterad programmering DAI2, förel. 9 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 The Network example A small, prototype social network. Supports a news feed with posts. Stores text posts and photo posts. MessagePost: multi-line text message. PhotoPost: photo and caption. Allows operations on the posts: E.g., search, display and remove. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Network objects Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Network classes add the obvious methods (getters and setters); not complete here - just examples Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Network object model database object will hold two collections: one for MessagePosts, one for PhotoPosts Vid repetitionen i nästa föreläsning: Gå nu till bild 11. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Class diagram class diagram is simple (ArrayList not shown in BlueJ) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Message-Post source code Objektorienterad programmering DAI2, förel. 9 public class MessagePost { private String username; private String message; private long timestamp; private int likes; private ArrayList<String> comments; public MessagePost(String author, String text) username = author; message = text; timestamp = System.currentTimeMillis(); likes = 0; comments = new ArrayList<String>(); } public void addComment(String text) ... public void like() ... public void display() ... ... Message-Post source code Just an outline extract from MessagePost code - nothing unusual, just an ordinary class Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Photo-Post source code Objektorienterad programmering DAI2, förel. 9 public class PhotoPost { private String username; private String filename; private String caption; private long timestamp; private int likes; private ArrayList<String> comments; public PhotoPost(String author, String filename, String caption) username = author; this.filename = filename; this.caption = caption; timestamp = System.currentTimeMillis(); likes = 0; comments = new ArrayList<String>(); } public void addComment(String text) ... public void like() … public void display() … ... Photo-Post source code Just an outline extract from PhotoPost code (how does it differ from MessagePost code?) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 public class NewsFeed { private ArrayList<MessagePost> messages; private ArrayList<PhotoPost> photos; ... public void show() for(MessagePost message : messages) { message.display(); System.out.println(); // empty line between posts } for(PhotoPost photo : photos) { photo.display(); Objektorienterad programmering DAI2, förel. 9 NewsFeed extract from database code. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Critique of Network Code duplication: MessagePost and PhotoPost classes very similar (large parts are identical) makes maintenance difficult/more work introduces danger of bugs through incorrect maintenance Code duplication in NewsFeed class as well. we note a lot of code duplication. this is one problem with this solution (there are others) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Using inheritance solution: inheritance. make superclass with common attributes, make subclasses TAVLAN: objektdiagram Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Using inheritance define one superclass : Post define subclasses for MessagePost and PhotoPost the superclass defines common attributes (via fields) the subclasses inherit the superclass attributes the subclasses add other attributes Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Inheritance hierarchies Objektorienterad programmering DAI2, förel. 9 Inheritance hierarchies inheritance hierarchies are nothing unusual. we see them all the time. (a masters student is a students is a person...) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Inheritance in Java no change here public class Post { ... } change here public class PhotoPost extends Post { ... } public class MessagePost extends Post { ... } Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Superclass public class Post { private String username; private long timestamp; private int likes; private ArrayList<String> comments; // constructor and methods omitted. } Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Subclasses public class MessagePost extends Post { private String message; // constructor and methods omitted. } public class PhotoPost extends Post { private String filename; private String caption; // constructor and methods omitted. } we add subclass fields; inherit superclass fields (note extends keyword) subclass objects will have all fields. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Inheritance and constructors Objektorienterad programmering DAI2, förel. 9 Inheritance and constructors public class Post { private String username; private long timestamp; private int likes; private ArrayList<String> comments; /** * Initialise the fields of the post. */ public Post(String author) username = author; timestamp = System.currentTimeMillis(); likes = 0; comments = new ArrayList<String>(); } // methods omitted how do we initialise the fields? superclass: nothing unusual. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Inheritance and constructors Objektorienterad programmering DAI2, förel. 9 Inheritance and constructors public class MessagePost extends Post { private String message; /** * Constructor for objects of class MessagePost */ public MessagePost(String author, String text) super(author); message = text; } // methods omitted subclass: must call superclass constructor! Must take values for all fields that we want to initialise.. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Superclass constructor call Objektorienterad programmering DAI2, förel. 9 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Adding more item types it is now much easier to add new types. common attributes do not need to be rewritten. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Deeper hierarchies when adding new types, the hierarchy may be extended Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Revised NewsFeed source code Objektorienterad programmering DAI2, förel. 9 Revised NewsFeed source code public class NewsFeed { private ArrayList<Post> posts; /** * Construct an empty news feed. */ public NewsFeed() posts = new ArrayList<Post>(); } * Add a post to the news feed. public void addPost(Post post) posts.add(post); ... avoids code duplication in the client class! note: code duplication in class NewsFeed removed as well! only on field, one ArrayList creation, on add method Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

New NewsFeed source code Objektorienterad programmering DAI2, förel. 9 /** * 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 ... } New NewsFeed source code ...and only one loop in list method. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 First, we had: public void addMessagePost( MessagePost message) public void addPhotoPost( PhotoPost photo) Now, we have: public void addPost(Post post) We call this method with: PhotoPost myPhoto = new PhotoPost(...); feed.addPost(myPhoto); Objektorienterad programmering DAI2, förel. 9 Subtyping Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Subclasses and subtyping Objektorienterad programmering DAI2, förel. 9 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Subtyping and assignment Objektorienterad programmering DAI2, förel. 9 Subtyping and assignment subclass objects may be assigned to superclass variables Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Vehicle v3 = new Bicycle(); Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Subtyping and parameter passing Objektorienterad programmering DAI2, förel. 9 Subtyping and parameter passing public class NewsFeed { public void addPost(Post post) ... } PhotoPost photo = new PhotoPost(...); MessagePost message = new MessagePost(...); feed.addPost(photo); feed.addPost(message); subclass objects may be used as actual parameters for the superclass Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Object diagram NewsFeed object will hold a single, mixed collection Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Class diagram Newsitem now only knows about Post rather than the subclasses. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Polymorphic variables Objektorienterad programmering DAI2, förel. 9 Polymorphic variables Object 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 Casting We can assign subtype to supertype … … but we cannot assign supertype to subtype! Vehicle v; Car c = new Car(); v = c; // correct c = v; // compile-time error! Casting fixes this: c = (Car) v; (only ok if the vehicle really is a Car!) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 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: ClassCastException if it isn't! Use it sparingly. TAVLAN: if ( v instanceof Car ) c = (Car)v; Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 The Object class All classes inherit from Object. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Polymorphic collections Objektorienterad programmering DAI2, förel. 9 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Polymorphic collections Objektorienterad programmering DAI2, förel. 9 Polymorphic collections A type parameter limits the degree of polymorphism: ArrayList<Post> Collection methods are then typed. Without a type parameter, ArrayList<Object> is implied. Likely to get an “unchecked or unsafe operations” warning. More likely to have to use casts. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Polymorphic collections (3) Objektorienterad programmering DAI2, förel. 9 Polymorphic collections (3) Ex. Whatch out for type cast errors! ArrayList box = new ArrayList(); Integer banana = new Integer(37); box.add(”apple”); box.add(banana); String fruit = (String)box.get(0); fruit = (String)box.get(1); Type cast Object to String Oops! The next element is an Integer Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Polymorphic collections (4) Objektorienterad programmering DAI2, förel. 9 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2

Objektorienterad programmering DAI2, förel. 9 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, DAT050, DAI2, 17/18, lp 1 DAT050, DAI2, 17/18, lp 2