FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.

Slides:



Advertisements
Similar presentations
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Advertisements

More on Classes Inheritance and Polymorphism
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Composite: Shapes Organisation State Observer Design Patterns.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 1 Inheritance University Of Ha’il.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
FEN KbP: Seminar 2/JML-Intro1 JML Introduktion Specifikation af en Personklasse.
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.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
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.
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)
Inheritance using Java
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
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.
More about inheritance Exploring polymorphism 5.0.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
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,
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
Object-Oriented Concepts
More about inheritance
10 More about inheritance
More about inheritance
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Lecture 19 - Inheritance (Contd).
Java Programming Language
More about inheritance
COS 260 DAY 18 Tony Gauvin.
Objects First with Java
CIS 199 Final Review.
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN IntroJava2006 AAU2 Dome Eksempel Lagrer information om videoer og CD-er: Masser af kode er ens i de to klasser

FEN IntroJava2006 AAU3 Nedarvning Fælles egenskaber (attributter og metoder) defineres på superklassen Item Subklasserne CD og Video arver alle egenskaber fra superklassen Angiver arv

FEN IntroJava2006 AAU4 Nedarvning kan give store hierarkier:

FEN IntroJava2006 AAU5 Inheritance: Dome Example

FEN IntroJava2006 AAU6 Inheritance in Java public class Item {... } public class CD extends Item {... } public class Video extends Item {... } no change here change here

FEN IntroJava2006 AAU7 Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }

FEN IntroJava2006 AAU8 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. }

FEN IntroJava2006 AAU9 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

FEN IntroJava2006 AAU10 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 }

FEN IntroJava2006 AAU11 More subtypes and deeper hierarchies

FEN IntroJava2006 AAU12 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); }...} Database source code avoids code duplication in client!

FEN IntroJava2006 AAU13 /** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for( int i= 0 ; i< ite ms.size() ; i++ ) { Item item = items.get(i) ; item.print(); System.out.println(); // empty line between items } Database source code

FEN IntroJava2006 AAU14 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility

FEN IntroJava2006 AAU15 Subtyping We have: public void addItem(Item theItem) We call this method with: Video myVideo = new Video(...); database.addItem(myVideo); Static type Dynamic type

FEN IntroJava2006 AAU16 Subtyping and Assignment Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables Static type Dynamic type

FEN IntroJava2006 AAU17 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 Dynamic type Static type

FEN IntroJava2006 AAU18 Object diagram

FEN IntroJava2006 AAU19 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.

FEN IntroJava2006 AAU20 The inheritance hierarchy Print()-method only defined in Item

FEN IntroJava2006 AAU21 Conflicting output CD: A Swingin' Affair (64 mins)* Frank Sinatra tracks: 16 my favourite Sinatra album video: O Brother, Where Art Thou? (106 mins) Joel & Ethan Coen The Coen brothers’ best movie! title: A Swingin' Affair (64 mins)* my favourite Sinatra album title: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie! What we want What we now have

FEN IntroJava2006 AAU22 print()-method in Class Item public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // Print details of this item to the text terminal public void print() { System.out.print(title + " (" + playingTime + " mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); } System.out.println(" " + comment); } Can of course only print it’s own fields

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

FEN IntroJava2006 AAU24 Method lookup – 1st version No inheritance or polymorphism. The obvious method is selected.

FEN IntroJava2006 AAU25 Method lookup – 2nd verison Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.

FEN IntroJava2006 AAU26 Method lookup – 3rd version Polymorphism and overriding. The ‘first’ version found is used.

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

FEN IntroJava2006 AAU28 Another example: Banking The bank has customers and accounts A customer has a collection of accounts The bank has standard accounts and budget accounts where an overdraft is allowed See the project: bankExamplebankExample

FEN IntroJava2006 AAU29 Interfaces Et interface er en klasse helt uden implementation, dvs.: –ingen attributter –ingen metodeimplementeringer –kun metodehoveder public interface ItemIF{ public String getTitle(); public void setComment(String comment); public String getComment(); public void setOwn(boolean ownIt); public boolean getOwn(); public void print();}

FEN IntroJava2006 AAU30 Hvorfor interfaces? Adskiller specifikation (hvad?) fra implementation (hvordan?) Kan bruges som statisk type, dvs. klientprogrammet behøver ikke kende implementationen (klassen) Klasser kan kun arve fra en klasse, men implementere flere interfaces Det er nødvendigt at kende til interfaces, da de bruges i udstrakt grad i Java’s biblioteker

FEN IntroJava2006 AAU31 Dome v. 2: Interface Implementerer Arver/extends

FEN IntroJava2006 AAU32 Implementering af et interface public abstract class Item implements ItemIF{ //attributter og constructor //evt yderligere metoder} public String getTitle(){…}; public void setComment(String comment){…}; public String getComment(){…}; public void setOwn(boolean ownIt){…}; public boolean getOwn(){…}; public void print(){…}; Metoder defineret i interfacet skal implementeres.

FEN IntroJava2006 AAU33 Interface som statisk type public class Database{ private ArrayList items; public Database(){ items = new ArrayList (); } public void addItem( ItemIF theItem){…} public void list(){ for(int i= 0; i<items.size(); i++) { ItemIF item = items.get(i); item.print(); } Alle klasser, som implementerer ItemIF kan bruges som dynamisk type