Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
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
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Inheritance Chapter 8.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Understanding class definitions Looking inside classes.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Inheritance using Java
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
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.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Programming Fundamentals 2: Inheritance/ F II Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures,
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
Object Oriented Software Development
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Polymorphism (generics) CSE 331 University of Washington.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Objects First With Java A Practical Introduction Using BlueJ Casting Week
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.
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.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
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
Sixth Lecture ArrayList Abstract Class and Interface
More about inheritance
Lecture 19 - Inheritance (Contd).
More about inheritance
Generics 27-Nov-18.
COS 260 DAY 18 Tony Gauvin.
Objects First with Java
CIS 199 Final Review.
Java Programming Language
Comp1202: Inheritance I Super and Sub-classes.
Generics 2-May-19.
CSE 143 Lecture 21 Advanced List Implementation
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Improving structure with inheritance

25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables

25/11/2004Lecture 7: Inheritance3 The DoME example "Database of Multimedia Entertainment" stores details about CDs and videos –CD: title, artist, # tracks, playing time, got- it, comment –Video: title, director, playing time, got-it, comment allows (later) to search for information or print lists

25/11/2004Lecture 7: Inheritance4 DoME objects

25/11/2004Lecture 7: Inheritance5 DoME classes

25/11/2004Lecture 7: Inheritance6 DoME object model

25/11/2004Lecture 7: Inheritance7 Class diagram

25/11/2004Lecture 7: Inheritance8 CD source code public class CD { private String title; private String artist; private String comment; public CD(String theTitle, String theArtist) { title = theTitle;artist = theArtist;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) []

25/11/2004Lecture 7: Inheritance9 Video source code public class Video { private String title; private String director; private String comment; public Video(String theTitle, String theDirect) { title = theTitle;director = theDirect;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) []

25/11/2004Lecture 7: Inheritance10 class Database { private ArrayList cds ; private ArrayList videos ;... public void list() { for(Iterator iter = cds.iterator(); iter.hasNext(); ) { CD cd = (CD)iter.next(); cd.print(); System.out.println(); // empty line between items } for(Iterator iter = videos.iterator(); iter.hasNext(); ) { Video video = (Video)iter.next(); video.print(); System.out.println(); // empty line between items } Database source code

25/11/2004Lecture 7: Inheritance11 Critique of DoME code duplication –CD and Video classes very similar (large part are identical) –makes maintenance difficult/more work –introduces danger of bugs through incorrect maintenance code duplication also in Database class

25/11/2004Lecture 7: Inheritance12 Using inheritance

25/11/2004Lecture 7: Inheritance13 Using inheritance define one superclass : Item define subclasses for Video and CD the superclass defines common attributes the subclasses inherit the superclass attributes the subclasses add own attributes

25/11/2004Lecture 7: Inheritance14 Inheritance hierarchies

25/11/2004Lecture 7: Inheritance15 Inheritance in Java public class Item {... } public class CD extends Item {... } public class Video extends Item {... } no change here change here

25/11/2004Lecture 7: Inheritance16 Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }

25/11/2004Lecture 7: Inheritance17 Subclasses public class CD extends Item { private String artist; private int numberOfTracks; // constructors and methods omitted. } public class Video extends Item { private String director; // constructors and methods omitted. }

25/11/2004Lecture 7: Inheritance18 public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; /** * Initialise the fields of the item. */ public Item(String theTitle, int time) { title = theTitle; playingTime = time; gotIt = false; comment = ""; } // methods omitted } Inheritance and constructors

25/11/2004Lecture 7: Inheritance19 Inheritance and constructors public class CD extends Item { private String artist; private int numberOfTracks; /** * Constructor for objects of class CD */ public CD(String theTitle, String theArtist, int tracks, int time) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } // methods omitted }

25/11/2004Lecture 7: Inheritance20 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.

25/11/2004Lecture 7: Inheritance21 Adding more item types

25/11/2004Lecture 7: Inheritance22 Deeper hierarchies

25/11/2004Lecture 7: Inheritance23 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility

25/11/2004Lecture 7: Inheritance24 public class Database{ private ArrayList items; /** * Construct an empty Database. */ public Database() { items = new ArrayList(); } /** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); }...} New Database source code avoids code duplication in client!

25/11/2004Lecture 7: Inheritance25 /** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); System.out.println(); // empty line between items } New Database source code

25/11/2004Lecture 7: Inheritance26 Subtyping First, we had: public void addCD(CD theCD) public void addVideo(Video theVideo)Now, we have: public void addItem(Item theItem) We call this method with: Video myVideo = new Video(...); database.addItem(myVideo);

25/11/2004Lecture 7: Inheritance27 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.)

25/11/2004Lecture 7: Inheritance28 Subtyping and assignment Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables

25/11/2004Lecture 7: Inheritance29 Subtyping and parameter passing public class Database { public void addItem(Item theItem) {... } Video video = new Video(...); CD cd = new CD(...); database.addItem(video); database.addItem(cd); subclass objects may be passed to superclass parameters

25/11/2004Lecture 7: Inheritance30 Object diagram

25/11/2004Lecture 7: Inheritance31 Class diagram

25/11/2004Lecture 7: Inheritance32 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.

25/11/2004Lecture 7: Inheritance33 The Object class All classes inherit from Object.

25/11/2004Lecture 7: Inheritance34 Polymorphic collections All collections are polymorphic. The elements are of type Object. public void add(Object element) public Object get(int index)

25/11/2004Lecture 7: Inheritance35 Casting revisited Can assign subtype to supertype. Cannot assign supertype to subtype! String s1 = myList.get(1); error! Casting fixes this: String s1 = (String) myList.get(1); (only if the element really is a String!)

25/11/2004Lecture 7: Inheritance36 Wrapper classes All objects can be entered into collections......because collections accept elements of type Object......and all classes are subtypes of Object. Great! But what about simple types?

25/11/2004Lecture 7: Inheritance37 Wrapper classes Simple types (int, char, etc) are not objects. They must be wrapped into an object! Wrapper classes exist for all simple types: simple typewrapper class intInteger floatFloat charCharacter...

25/11/2004Lecture 7: Inheritance38 Wrapper classes int i = 18; Integer iwrap = new Integer(i); myCollecton.add(iwrap);... Integer element = (Integer) myCollection.get(0); int value = element.intValue() wrap the int value add the wrapper retrieve the wrapper unwrap

25/11/2004Lecture 7: Inheritance39 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).

25/11/2004Lecture 7: Inheritance40 Concepts Inheritance Superclass Subclass Base Class Object Subtyping Type casting Polymorphic variables Inheritance Hierarchies