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

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
Classes and Objects in Java
Introduction to Java 2 Programming
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Objectives: Generate and describe sequences. Vocabulary:
UNITED NATIONS Shipment Details Report – January 2006.
12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
2 Copyright © 2005, Oracle. All rights reserved. Defining Object-Oriented Principles.
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
EU market situation for eggs and poultry Management Committee 20 October 2011.
Object Oriented Programming with Java
VOORBLAD.
21-Aug-14 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
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.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
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 and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Modern Programming Tools And Techniques-I
Reusing Code with Inheritance and Polymorphism
Presentation transcript:

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

10-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Define inheritance Use inheritance to define new classes Provide suitable constructors Override methods in the superclass Describe polymorphism Use polymorphism effectively

10-3 Copyright © 2005, Oracle. All rights reserved. Key Object-Oriented Components Inheritance Constructors referenced by subclass Polymorphism Inheritance as an OO fundamental InventoryItem MovieGameVcr Superclass Subclasses

10-4 Copyright © 2005, Oracle. All rights reserved. Example of Inheritance The InventoryItem class defines methods and variables. Movie extends InventoryItem and can: –Add new variables –Add new methods –Override methods in InventoryItem class InventoryItem Movie

10-5 Copyright © 2005, Oracle. All rights reserved. Specifying Inheritance in Java Inheritance is achieved by specifying which superclass the subclass extends. Movie inherits all the variables and methods of InventoryItem. If the extends keyword is missing, then the java.lang.Object is the implicit superclass. public class InventoryItem { … } public class Movie extends InventoryItem { … }

10-6 Copyright © 2005, Oracle. All rights reserved. Defining Inheritance by Using Oracle JDeveloper 10g When specifying a class, JDeveloper asks for its superclass: JDeveloper generates the code automatically.

10-7 Copyright © 2005, Oracle. All rights reserved. What Does a Subclass Object Look Like? A subclass inherits all the instance variables of its superclass. Movie title length price condition public class Movie extends InventoryItem { private String title; private int length; … } public class InventoryItem { private float price; private String condition; … }

10-8 Copyright © 2005, Oracle. All rights reserved. Default Initialization What happens when a subclass object is created? If no constructors are defined: –First, the default no-arg constructor is called in the superclass. –Then, the default no-arg constructor is called in the subclass. Movie movie1 = new Movie(); Movie title length price condition

10-9 Copyright © 2005, Oracle. All rights reserved. The super Reference Refers to the base, top-level class Is useful for calling base class constructors Must be the first line in the derived class constructor Can be used to call any base class methods

10-10 Copyright © 2005, Oracle. All rights reserved. The super Reference Example public class InventoryItem { InventoryItem(String cond) { System.out.println("InventoryItem"); … } class Movie extends InventoryItem { Movie(String title) { Movie(String title, String cond) {super(cond); … System.out.println("Movie"); } Base class constructor Calls base class constructor

10-11 Copyright © 2005, Oracle. All rights reserved. Using Superclass Constructors Use super() to call a superclass constructor: public class InventoryItem { InventoryItem(float p, String cond) { price = p; condition = cond; } … public class Movie extends InventoryItem { Movie(String t, float p, String cond) { super(p, cond); title = t; } …

10-12 Copyright © 2005, Oracle. All rights reserved. Notes page

10-13 Copyright © 2005, Oracle. All rights reserved. Specifying Additional Methods The superclass defines methods for all types of InventoryItem. The subclass can specify additional methods that are specific to Movie. public class InventoryItem { public float calcDeposit()… public String calcDateDue()… … public class Movie extends InventoryItem { public void getTitle()… public String getLength()…

10-14 Copyright © 2005, Oracle. All rights reserved. Notes page

10-15 Copyright © 2005, Oracle. All rights reserved. Overriding Superclass Methods A subclass inherits all the methods of its superclass. The subclass can override a method with its own specialized version. –The subclass method must have the same signature and semantics as the superclass method. public class InventoryItem { public float calcDeposit(int custId) { if … return itemDeposit; } public class Vcr extends InventoryItem { public float calcDeposit(int custId) { if … return itemDeposit; }

10-16 Copyright © 2005, Oracle. All rights reserved. Notes page

10-17 Copyright © 2005, Oracle. All rights reserved. Invoking Superclass Methods If a subclass overrides a method, then it can still call the original superclass method. Use super.method() to call a superclass method from the subclass. public class InventoryItem { public float calcDeposit(int custId) { if … return 33.00; } public class Vcr extends InventoryItem { public float calcDeposit(int custId) { itemDeposit = super.calcDeposit(custId); return (itemDeposit + vcrDeposit); }

10-18 Copyright © 2005, Oracle. All rights reserved. Notes page

10-19 Copyright © 2005, Oracle. All rights reserved. Example of Polymorphism in Java Recall that the java.lang.Object class is the root class for all Java Class. Methods in the Object class are inherited by its subclasses. The toString() method is most commonly overridden to achieve polymorphic behavior. For example: public class InventoryItem { public String toString() { return "InventoryItem value"; } InventoryItem item = new InventoryItem(); System.out.println(item); // toString() called

10-20 Copyright © 2005, Oracle. All rights reserved. Treating a Subclass as Its Superclass A Java object instance of a subclass is assignable to its superclass definition. You can assign a subclass object to a reference that is declared with the superclass. The compiler treats the object via its reference (that is, in terms of its superclass definition). The JVM run-time environment creates a subclass object, executing subclass methods, if overridden. public static void main(String[] args) { InventoryItem item = new Vcr(); double deposit = item.calcDeposit(); }

10-21 Copyright © 2005, Oracle. All rights reserved. Browsing Superclass References by Using Oracle JDeveloper 10g Oracle JDeveloper makes it easy to browse the contents of your superclass. 3 12

10-22 Copyright © 2005, Oracle. All rights reserved. Acme Video and Polymorphism Acme Video started renting only videos. Acme Video added games and Vcrs. What is next? Polymorphism solves the problem.

10-23 Copyright © 2005, Oracle. All rights reserved. ShoppingBasket Using Polymorphism for Acme Video void addItem(InventoryItem item) { // this method is called each time // the clerk scans in a new item float deposit = item.calcDeposit(); … } InventoryItem Vcr Movie calcDeposit(){…}

10-24 Copyright © 2005, Oracle. All rights reserved. Notes page

10-25 Copyright © 2005, Oracle. All rights reserved. Using the instanceof Operator You can determine the true type of an object by using an instanceof operator. An object reference can be downcast to the correct type, if necessary. public void aMethod(InventoryItem i) { … if (i instanceof Vcr) ((Vcr)i).playTestTape(); }

10-26 Copyright © 2005, Oracle. All rights reserved. Limiting Methods and Classes with final You can mark a method as final to prevent it from being overridden. You can mark a whole class as final to prevent it from being extended. public final class Color { … } public final boolean checkPassword(String p) { … }

10-27 Copyright © 2005, Oracle. All rights reserved. Ensuring Genuine Inheritance Inheritance must be used only for genuine is a kind of relationships: –It must always be possible to substitute a subclass object for a superclass object. –All methods in the superclass must make sense in the subclass. Inheritance for short-term convenience leads to problems in the future.

10-28 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned the following: A subclass inherits all the variables and methods of its superclass. You can specify additional variables and methods and override methods. A subclass can call an overridden superclass method by using super. Polymorphism ensures that the correct version of a method is called at run time.

10-29 Copyright © 2005, Oracle. All rights reserved. Practice 10: Overview This practice covers: Defining subclasses of Customer Providing subclass constructors Adding new methods in the subclasses Overriding existing superclass methods

10-30 Copyright © 2005, Oracle. All rights reserved. Notes Page for Practice 10

10-31 Copyright © 2005, Oracle. All rights reserved. Practice 10: Notes

10-32 Copyright © 2005, Oracle. All rights reserved. Practice 10: Notes

10-33 Copyright © 2005, Oracle. All rights reserved. Practice 10: Notes

10-34 Copyright © 2005, Oracle. All rights reserved. Practice 10: Notes

10-35 Copyright © 2005, Oracle. All rights reserved. Practice 10: Notes

10-36 Copyright © 2005, Oracle. All rights reserved.