Improving structure with inheritance

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Improving structure with inheritance (Chapters 8 and 9)
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Grouping Objects 3 Iterators, collections and the while loop.
Inheritance Chapter 8.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Understanding class definitions Looking inside classes 3.0.
Grouping Objects 1 Introduction to Collections.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More about inheritance Exploring polymorphism 5.0.
More about inheritance Exploring polymorphism 3.0.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
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.
Programming Fundamentals 2: Inheritance/ F II Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures,
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Object Oriented Software Development
Understanding class definitions
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
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.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
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.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
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.
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Grouping objects Introduction to collections 5.0.
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 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
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.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
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.
9 Improving structure with inheritance
Objects First with Java Introduction to collections
Inheritance and Polymorphism
10 More about inheritance
More about inheritance
COS 260 DAY 19 Tony Gauvin.
Chapter 9 Inheritance and Polymorphism
COS 260 DAY 19 Tony Gauvin.
Lecture 19 - Inheritance (Contd).
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Understanding class definitions
Collections and iterators
Objects First with Java
COS 260 DAY 23 Tony Gauvin.
Comp1202: Inheritance I Super and Sub-classes.
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Improving structure with inheritance Objects First with Java Improving structure with inheritance 5.0 © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Network objects one object per post; each object stores details for one particular post. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Network classes add the obvious methods (getters and setters); not complete here - just examples Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Network object model database object will hold two collections: one for MessagePosts, one for PhotoPosts Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Class diagram class diagram is simple (ArrayList not shown in BlueJ) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Message-Post source code Objects First with Java 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Photo-Post source code Objects First with Java 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?) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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(); Objects First with Java NewsFeed extract from NewsFeed code. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Using inheritance solution: inheritance. make superclass with common attributes, make subclasses Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Inheritance hierarchies Objects First with Java Inheritance hierarchies inheritance hierarchies are nothing unusual. we see them all the time. (a masters student is a student is a person...) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Inheritance in Java no change here public class Post { ... } change here public class PhotoPost extends Post { ... } syntax for inheritance: ‘extends’ keyword public class MessagePost extends Post { ... } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Superclass public class Post { private String username; private long timestamp; private int likes; private ArrayList<String> comments; // constructor and methods omitted. } we define common fields in superclass Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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 subclass objects will have all fields. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Inheritance and constructors Objects First with Java Inheritance and constructors public class Post { private String username; private long timestamp; private int likes; private ArrayList<String> comments; /** * Initialize 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 initialize the fields? superclass: nothing unusual. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Inheritance and constructors Objects First with Java Inheritance and constructors public class MessagePost extends Post { private String message; /** * Constructor for objects of class MessagePost */ public CD(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 initialize. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Superclass constructor call Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Adding more item types it is now much easier to add new types. common attributes do not need to be rewritten. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Deeper hierarchies when adding new types, the hierarchy may be extended Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Revised NewsFeed source code Objects First with Java 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, one add method Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

New NewsFeed source code Objects First with Java /** * 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 the show method. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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); Objects First with Java Subtyping Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Subclasses and subtyping Objects First with Java 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 .) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Subtyping and assignment Objects First with Java Subtyping and assignment subclass objects may be assigned to superclass variables Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Vehicle v3 = new Bicycle(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Subtyping and parameter passing Objects First with Java 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Object diagram NewsFeed object will hold a single, mixed collection Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Class diagram NewsItem now only knows about Post rather than the subclasses. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Polymorphic variables Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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!) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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 extending Variables can hold subtype objects. Subtypes can be used wherever supertype objects are expected (substitution). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling