Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.

Slides:



Advertisements
Similar presentations
FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
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. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance Chapter 8.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Polymorphism & Interfaces
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
What is inheritance? It is the ability to create a new class from an existing class.
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 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Programming Fundamentals 2: Inheritance/ F II Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures,
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
M1G Introduction to Programming 2 5. Completing the program.
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.
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.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
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.
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,
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
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
Java Unit 11: Inheritance I
More about inheritance
Lecture 19 - Inheritance (Contd).
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Two big words Inheritance Polymorphism
OO Design with Inheritance
Objects First with Java
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Chapter 8 Improving Structure with Inheritance

The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and videos We will want to be able to perform actions on each of these lists of items.

Desired Functionality Enter information about CDs and videos Store information permanently Search functionality Print lists of all CDs or videos

Desired Information Title of album Artist Number of tracks Total Playing time “Got it” flag Comment Title of video Name of director Total Playing time “Got it” flag Comment

Design Discussion Each of the private fields would have a setter and a getter The each list of CDs and videos could be stored in a separate array list Do you see any issues with this approach? DoME Source Code

Major Issue Code duplication There is code duplication in almost every aspect of every class With the two ArrayLists we have to duplicate every method. With repeated information we have to repeat the same code in both the CD and video class What happens when we want to add books?

Inheritance Inheritance allows us to define one class as an extension of another Inheritance allows subclasses to get all the fields and methods from the superclass. In this way, we can put all the common information in one class and inherit from it Then we can put the specialized information in the subclasses

Better Classes for DoME

Inheritance Hierarchies Animals Mammal Dog PoodleDalmatian Cat Bird ChickenSparrow

Inheritance and Java There are some syntax issues when dealing with Java and inheritance We use the keyword extends to indicate that one class inherits from another class We may choose to use different access modifiers within the base class to allow for easier access in the child classes We need ways to initialize the super class

Inheritance and Java public class Item { private String title; private int playingTime; private boolean gotIt; private String Comment; … } public class CD extends Item { private String artist; private int numberOfTracks; … }

Inheritance and Access Rights Just like any other class a subclass of a superclass is not allowed to access any private data; it must use setters and getters It does not need to use a dot in front of any methods It can call them as if they were their own We will come back to this issue when later and introduce a protected access modifier

Inheritance and Initialization When we create an object the constructor is called and the state of the object is set. But how do we set the state of the superclass? In the first line of the subclass’s constructor we use the key word super and supply any necessary parameters to the superclass’s constructor

Example public class Item { … public Item( String theTitle, int time ) { title = theTitle; playingTime = time; gotIt = false; comment = “”; } … } public class CD extend Item { … public CD( String theTitle, String theArtist, int tracks, int time ) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } … }

Things to Notice The constructor for the CD receives the information necessary to create an item in addition to the information need to create the CD The key word super is the call to Item’s constructor and we pass in the information it wants The first line of the subclass’s constructor must always be to the superclass’s constructor through the key word super

Advantages of Inheritance Avoiding code duplication Code reuse Easier maintenance Extensibility

Subtyping Let’s take a look at the Database code since we have changed to inheritance DoME Example V2 It is much simpler The reason is subtyping

Subtyping Again When we have said in the past that when you call a method, the actual parameters must match the type of the formal parameters We could have said must match the type or be a subtype of the formal paramaters We can now have one ArrayList that will hold items So addItem will take an item or any of its subclasses as a parameter.

Subtyping and Assignments We are allowed to assign to a variable any object that matches its type or is a subtype of its type These are allowed Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Vehicle v3 = new Bike(); This is not Car c1 = new Vehicle(); Vehicle CarBike

Polymorphism Polymorphism, for now, is an idea that allows a variable to be able to be more than one thing at different times. It allows us to simplify our print code for the database. We will come back to this important idea later

The Object class In Java all objects inherit from the Object class. We don’t have to do anything, it is automatic. This is how ArrayLists can hold any type of object. They expect an Object as their parameters. Since all objects inherit from this common base, we can pass any object to a collection in Java That is also why, it gives us an object back and we have to cast it back to its particular type.

HW Chapter 8: 8.3, 8.6, 8.11, 8.13, 8.17