Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0.

Slides:



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

1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
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.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
More about inheritance Exploring polymorphism 5.0.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
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.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
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.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance Dwight Deugo Nesa Matic
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
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.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
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.
Coen Brothers Joel & Ethan.
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.
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 –
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
OOP: Encapsulation &Abstraction
More about inheritance
10 More about inheritance
More about inheritance
Java Unit 11: Inheritance I
An Introduction to Inheritance
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Lecture 19 - Inheritance (Contd).
Inheritance, Polymorphism, and Interfaces. Oh My
More about inheritance
Building Java Programs
Java – Inheritance.
Objects First with Java
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Classes in Java Specifics for Java Copyright Curt Hill.
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

2 media-v2

3 Conflicting output CD: A Swingin' Affair (64 mins)* My favourite Sinatra album Frank Sinatra Tracks: 16 DVD: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie! Joel & Ethan Coen 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

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

5 The solution: overriding Superclasses and their subclasses can define methods with the same name. We shall see how this can be used.

6 Method lookup No inheritance. The method is selected.

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

8 Method lookup Overriding. The Subclass method overrides the superclass version.

9 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. Overriding methods take precedence.

10 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.methodName(...) –Compare with the use of super in constructors.

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

12 Review Methods can be overridden in a subclass Overriding methods take precedence Superclass methods can be called using the keyword super