More about inheritance

Slides:



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

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
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.
Inheritance in collections Week Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution.
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#
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.
Improving structure with inheritance (Chapters 8 and 9)
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
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.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
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,
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
10 More about inheritance
More about inheritance
03/10/14 Chapter 9 Inheritance.
COS 260 DAY 19 Tony Gauvin.
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
COS 260 DAY 19 Tony Gauvin.
Lecture 19 - Inheritance (Contd).
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
More about inheritance
Java – Inheritance.
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Java Programming, Second Edition
Fundaments of Game Design
Inheritance and Polymorphism
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Improving structure with inheritance
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

More about inheritance Exploring polymorphism 2.0

Main concepts to be covered method polymorphism static and dynamic type overriding dynamic method lookup protected access

The inheritance hierarchy

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

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. Database cannot find a print method in Item.

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 What is the type of c1? Car c1 = new Car(); What is the type of v1? Vehicle v1 = new Car();

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. for(Item item : items) { item.print(); // Compile-time error. }

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 variable is accessed. 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.

Protected access Private access in the superclass may be too restrictive for a subclass. The closer inheritance relationship is supported by protected access. Protected access is more restricted than public access. We still recommend keeping fields private. Define protected accessors and mutators.

Access levels

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.