Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism."— Presentation transcript:

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

2 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

3 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

4 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

5 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 { … }

6 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.

7 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; … }

8 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

9 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-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

11 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; } …

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

13 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()…

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

15 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; }

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

17 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); }

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

19 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

20 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(); }

21 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

22 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.

23 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(){…}

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

25 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(); }

26 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) { … }

27 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.

28 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.

29 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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google