1 / 52

Chapter 10 Inheritance, Polymorphism, and Scope

Chapter 10 Inheritance, Polymorphism, and Scope. Knowledge Goals. Understand the hierarchical nature of classes in object-oriented programming Understand the concept of inheritance in a class hierarchy Recognize the distinction between overriding and hiding in Java

vcheung
Download Presentation

Chapter 10 Inheritance, Polymorphism, and Scope

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Inheritance, Polymorphism, and Scope

  2. Knowledge Goals • Understand the hierarchical nature of classes in object-oriented programming • Understand the concept of inheritance in a class hierarchy • Recognize the distinction between overriding and hiding in Java • Understand the concept of polymorphism • Know and understand the access rules for Java classes

  3. Knowledge Goals • Understand the concept of overloading in Java • Understand the distinction between an abstract class and an interface • Explain the difference between static and dynamic binding • Understand the role of interfaces • Understand the role of the Comparable interface

  4. Skill Goals • Identify the interface components of a class in a hierarchy • Draw a UML diagram representing a class hierarchy • Design a derived class to extend an existing class hierarchy • Implement a derived class using inheritance

  5. Skill Goals • Use the keywords super and this to disambiguate references • Derive a class from an abstract class • Implement an interface • Determine the class of a object

  6. Inheritance

  7. Inheritance Superclass Hierarchy Subclasses Executive Office Floor is derived from Empty Office Floor Empty Office Floor is derived from Basic Empty Floor

  8. Inheritance • Inheritance • A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class • Superclass • A class that is extended by one or more derived classes • Derived class (subclass) • The class that inherits; it is specialized by adding properties specific to it

  9. WheeledVehicle Boat Car Bicycle FourDoor Inheritance Hierarchy Example Vehicle TwoDoor Every Car “is a” WheeledVehicle.

  10. Address object hierarchy Object Address HomeAddress CompanyAddress WorkAddress BoxAddress

  11. Inheritance Class Name: Superclass: Subclasses: CompanyAddress Address BoxAddress Responsibilities Collaborations

  12. Inheritance Superclass Derived class (subclass)

  13. Inheritance Derived class

  14. Inheritance Superclass Note constructors are not inherited

  15. Inheritance All classes extend Object by default

  16. Inheritance • Override • To provide an instance method in a derived class that has the same form of heading as an instance method in its superclass • the method in the derived class overrides (redefines) the method in the superclass • class methods can not be overridden For example, whenever you write a toString method, it overrides thetoStringmethod in Object

  17. Inheritance Hiding

  18. Inheritance • Hide • To provide a field in a derived class that has the same name as one in its superclass or a class method that has the same form of heading as a class method in its superclass; the field or class method hide the corresponding component in the superclass Override: Instance methods Hid: Fields or class methods

  19. Inheritance • public class Example • { • char letter; • public static String lineIs(); • … • } • public class ExtExample extends Example • { • char letter; • public static String lineIs(); • … • } Hiding or overriding?

  20. Inheritance • public class Example • { • char letter; • public String lineIs(); • … • } • public class ExtExample extends Example • { • String letter; • public String lineIs(); • … • } Hiding or overriding?

  21. Inheritance • Overridingvs.Hiding • Weoverridean instance method of a superclass by providing an instance method in a derived class with the same form of heading • Wehidea data field of a superclass by providing a field in a derived class with the same name • We hide a class method of a superclass by providing a class method with the same heading What exactly is "same heading?"

  22. Inheritance • Signature • The distinguishing features of a method heading: the combination of the method name with the number and types(s) of its parameters in their given order • Overloading • The repeated use of a method name with a different signature

  23. Inheritance • Are these signatures the same? • public static void someName(int formal1, int formal2, int formal3) • public static void someName(int formal1, double formal2, • int formal3) • public static void someName(double formal1, int formal2, • int formal3) • public static void someName(int formal1, int formal2, • String formal3)

  24. Inheritance • What about these? • public void aName(int param1, double param2, • String param3) • public void aName(int large, double medium, • String small) • public void aName(int red, double green, • String blue) • public void aName(int thing1, double thing2, • String hatCat)

  25. Inheritance • And these? • public static void aName(int param1, double param2, • String param3) • int aName(int large, double medium, String small) • protected double aName(int red, double green, • String blue) • private static String aName(int thing1, double thing2, • String hatCat)

  26. Inheritance • super • A keyword that • when followed by a parameter list refers to the constructor in the super class with the same signature • when followed by a field or method identifier, refers to an overridden or hidden method or field • A superclass hidden method can also be called by giving the superclass name and the method name with a dot in between

  27. Inheritance • To what do each of these refer? • super(); • super(Scanner in); • someInt = super.someInt; • super.instanceMethod(); • super.classMethod(); • super.classMethod();

  28. Scope Scope (of access) The region of program code where it is legal to reference or use an identifier Scope rules The rules that determine where in a program an identifier may be referenced, given the point where the identifier is declared and its specific access modifier That is, who knows what, where, and when within the text of a program

  29. Scope • Internal scope (within a class) • Any identifier declared in a class can be used anywhere within the class except • you can't use one class variable to initialize another before the first has been defined • within a block, a local identifier hides a class member of the same name (called shadowing) What if I want to access the non-local identifier?

  30. Scope • public class SomeClass • { • static final int CONST = 5; • int var = –1; • int param = 0; • public int someMethod(int param) • { • int var; • final int CONST = SomeClass.CONST*2; • if (param > this.param) var = param * CONST; • else var = this.param * SomeClass.CONST; • return var; • } • } Look at the use of CONST, param, and var

  31. Scope • External scope • Access to class members from code outside the class; controlled by access modifiers • Access modifiers • Remember them?

  32. Scope • public • protected • ( no access modifier ) • “package access” • private public protected package private

  33. Scope Public access

  34. Scope Protected assess

  35. Scope Package access within/between packages

  36. Scope Private access

  37. Scope • External accesspublic protected (default) private • package • Same package yes yes yes no • Derived class in yes yes no no • another package (inheritance • only) • User code yes no no no

  38. Scope • A final word… • A class marked final cannot be extended • A method marked final cannot be overridden or hidden

  39. Polymorphism • Polymorphism • An operation that has multiple meanings depending on the call of object to which it is bound The instance to which toString is applied determines which is called Examples: Object toString Name toString Address toString Date toString

  40. Polymorphism • Abstract • A modifier of a class or field that indicates it is incomplete and must be fully defined in a derived class Method pay must be defined in a class that extends Worker abstract public class Worker { float hours; float rate; abstract public float pay(); … }

  41. Polymorphism • The rest of the puzzle • Reference • array interface class

  42. Polymorphism • Interface (in Java) • A model for a class that specifies the fields and methods that must be present in a class that implements the interface • Implementation (in Java) • A class containing the definitions of the methods specified in an interface This is the last definition of interface!

  43. Polymorphism • Yes, but what is a Java interface? • public interface comparable • { • int compareTo(Object obj); • } • returns <0, 0, >0 depending on the relationship of the instance to the parameter This should be familiar

  44. Polymorphism implements clause • public class Time implements Comparable • { • … • publicint compareTo(Object other) • { • Time otherTime = (Time) other; • if (hours < otherTime.hours) • … • } • } define method casts Object to Time Why must the argument be type cast?

  45. Polymorphism • Binding • Associating a name or symbol with appropriate code • Binding time • The time at which the association (binding) is done • Static binding • Binding is done at compile time • Dynamic binding • Binding is done at run time Which is more efficient, dynamic or static binding? Why?

  46. Polymorphism • A useful operator: instanceof • if (myPhone instanceof BusinesPhone) • newPhone = in.inutbusinessPhone() • else • newPhone = in.inputPhone() • A useful method: getClass() • myPhone.getClass().getSimpleName()

  47. Polymorphism • Two useful methods: • getClass() & getSimpleName() • if (myPhone.getClass().getSimpleName().equals("Phone")) instance returns returns name class of of class of instance instance

  48. Extras I had a machine and an award named after me; I was on Time's list of the 100 most influential persons in the 20th century Who am I?

  49. Extras - GUI Track • Adding input fields to a JFrame object requires • declaring and instantiating a JLabel object for a promppt • adding the label to the pane • declaring and instantiating a JTextField object • adding the field to the pane • getting the input from the field

  50. Extras - GUI Track JFrame object with an input field

More Related