Lecture 19 - Inheritance (Contd).

Slides:



Advertisements
Similar presentations
OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
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.
1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
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.
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 Abstract Classes and Interfaces Chapter 10.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
More about inheritance Exploring polymorphism 5.0.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
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,
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
More about inheritance Exploring polymorphism 5.0.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
Inheritance ndex.html ndex.htmland “Java.
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 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
9 Improving structure with inheritance
Chapter 11 Inheritance and Polymorphism
Inheritance Based on slides by Ethan Apter
Inheritance and Polymorphism
More about inheritance
10 More about inheritance
More about inheritance
COS 260 DAY 19 Tony Gauvin.
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
COS 260 DAY 19 Tony Gauvin.
Java Programming Language
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Advanced Java Programming
Java – Inheritance.
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Objects First with Java
Inheritance and Polymorphism
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Lecture 19 - Inheritance (Contd)

** ‘IS-A’ relationship Using inheritance Constructor? solution: inheritance. make superclass with common attributes, make subclasses ** ‘IS-A’ relationship

Using inheritance define one superclass : Item define subclasses: Video and CD the superclass defines common attributes the subclasses inherit the superclass attributes the subclasses add own attributes

Subtitution First, we had: Now, we have: public void addCD(CD theCD) public void addVideo(Video theVideo) Now, we have: public void addItem(Item theItem) Subtitution Objects of subclasses can be used where objects of supertypes are required. (This is called substitution .)

New Database source code 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); ... avoids code duplication in client! note: code duplication in class Database removed as well! only on field, one ArrayList creation, on add method

New Database source code /** * 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 ...and only one loop in list method.

Assignment Item i1 = new Item(); Item i2 = new CD(); Item i3 = new Video(); // Illegal Assignments * CD c1 = new Item(); CD c2 = new Video(); subclass objects may be assigned to superclass variables (Substitution)

Casting and Assignment Item anItem1; //superclass CD cd1; //subclass cd1 = new CD (); anItem1 = cd1; // subtype object can be assigned to the supertype - substitution Item anItem2; CD cd2; anItem = new CD(); cd2 = (CD) anItem; // ok superclass object can be assigned to subtype with a cast Video v1; CD cd3; cd3 = new CD(); v1 = cd3; //error – compile time … (compilation error) Item anItem3 = cd3; v1 = (Video) anItem3; // will compile, but runtime error

DoMe Example (contd)

Using inheritance solution: inheritance. make superclass with common attributes, make subclasses

The problem The print method in Item only prints the common fields. Inheritance is a one-way street: A subclass inherits the superclass fields. The superclass knows nothing about its subclass’s fields.

Attempting to solve the problem Place print where it has access to the information it needs. Each subclass has its own version. But Item’s fields are private.

New Database source code /** * 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 ...and only one loop in list method. No print method in Item: Code won’t compile

Static type and dynamic type A more complex type hierarchy requires further concepts to describe it. Some new terminology: static type dynamic type method dispatch/lookup

Static and dynamic type The declared type of a variable is its static type. The type of the object a variable refers to is its dynamic type. The compiler’s job is to check for static-type violations.

Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking.

Overriding Superclass and subclass define methods with the same signature. Each has access to the fields of its class. Superclass satisfies static type check. Subclass method is called at runtime – it overrides the superclass version. What becomes of the superclass version?

Method lookup No inheritance or polymorphism. The obvious method is selected.

Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.

Method lookup Polymorphism and overriding. The ‘first’ version found is used.

Method lookup summary The object stored in the variable is found. The class of the object is found. The class is searched for a method match. If no match is found, the superclass is searched. This is repeated until a match is found, or the class hierarchy is exhausted. Overriding methods take precedence.

Super call in methods Overridden methods are hidden ... ... but we often still want to be able to call them. An overridden method can be called from the method that overrides it. super.method(...) Compare with the use of super in constructors.

Calling an overridden method public class CD { ... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); } ...

Method polymorphism We have been discussing polymorphic method dispatch. A polymorphic variable can store objects of varying types. Method calls are polymorphic. The actual method called depends on the dynamic object type.

Review The declared type of a variable is its static type. Compilers check static types. The type of an object is its dynamic type. Dynamic types are used at runtime. Methods may be overridden in a subclass. Method lookup starts with the dynamic type. Protected access supports inheritance.