Improving structure with inheritance 6.0. 2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,

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)
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 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
More about inheritance Exploring polymorphism 5.0.
More about inheritance Exploring polymorphism 3.0.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Session 6 Comments on Lab 3 & Implications of Inheritance.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
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
Chapter 20 Generic Classes and Methods
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).
Extending Classes.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Comp1202: Inheritance I Super and Sub-classes.
Review: libraries and packages
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Improving structure with inheritance 6.0

2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

3 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

4 Network objects © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

5 Network classes © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

6 Network object model © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

7 Class diagram © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

8 Message- Post source code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class MessagePost{ private String username; private String message; private long timestamp; private int likes; private ArrayList comments; public MessagePost(String author, String text) { username = author;message = text;timestamp = System.currentTimeMillis();likes = 0;comments = new ArrayList<>(); } public void addComment(String text)... public void like()... public void display()......} Just an outline

9 public class PhotoPost{ private String username; private String filename; private String caption; private long timestamp; private int likes; private ArrayList 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<>(); } public void addComment(String text)... public void like() … public void display() …...} Photo- Post source code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. Just an outline

10 NewsFeed © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class NewsFeed{ private ArrayList messages ; private ArrayList photos ;... public void show() { for( MessagePost message : messages ) { message.display (); System.out.println(); // empty line between posts } for( PhotoPost photo : photos ) { photo.display (); System.out.println(); // empty line between posts }

11 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

12 Using inheritance © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

13 Using inheritance define one superclass : Post define subclasses for MessagePost and PhotoPost the superclass defines common attributes (via fields) the subclasses inherit the superclass characteristics the subclasses add other characteristics © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

14 Inheritance hierarchies © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

15 Inheritance in Java © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class Post {... } public class MessagePost extends Post {... } public class PhotoPost extends Post {... } no change here change here

16 Superclass © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class Post { private String username; private long timestamp; private int likes; private ArrayList comments; // constructor and methods omitted. }

17 Subclasses © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. 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. }

18 Inheritance and constructors © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class Post { private String username; private long timestamp; private int likes; private ArrayList comments; /** * Initialise the fields of the post. */ public Post(String author) { username = author; timestamp = System.currentTimeMillis(); likes = 0; comments = new ArrayList<>(); } // methods omitted }

19 Inheritance and constructors © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. 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 }

20 Superclass constructor call Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) –only compiles if the superclass has a constructor without parameters Must be the first statement in the subclass constructor. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

21 Adding more item types © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

22 Deeper hierarchies © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

23 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

24 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.) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

25 Subtyping and assignment © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables

26 Revised NewsFeed source code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class NewsFeed { private ArrayList posts ; /** * Construct an empty news feed. */ public NewsFeed () { posts = new ArrayList <> (); } /** * Add a post to the news feed. */ public void add Post ( Post post ) { post s.add( post ); }...} avoids code duplication in the client class!

27 New NewsFeed source code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. /** * 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... }

28 Subtyping © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. First, we had: public void add MessagePost ( MessagePost message ) public void add PhotoPost ( PhotoPost photo ) Now, we have: public void add Post ( Post post ) We call this method with: PhotoPost my Photo = new PhotoPost (...); feed.add Post (my Photo );

29 Subtyping and parameter passing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. 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

30 Object diagram © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

31 Class diagram © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

32 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

33 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; (but only ok if the vehicle really is a Car !) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

34 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

35 The Object class © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. All classes inherit from Object.

36 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

37 Polymorphic collections A type parameter limits the degree of polymorphism: ArrayList Collection methods are then typed. Without a type parameter, ArrayList is implied. Likely to get an “unchecked or unsafe operations” warning. More likely to have to use casts. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

38 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). © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.