530 likes | 547 Views
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
E N D
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 • Understand the concept of polymorphism • Know and understand the access rules for Java classes
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
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
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
Inheritance Superclass Hierarchy Subclasses Executive Office Floor is derived from Empty Office Floor Empty Office Floor is derived from Basic Empty Floor
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
WheeledVehicle Boat Car Bicycle FourDoor Inheritance Hierarchy Example Vehicle TwoDoor Every Car “is a” WheeledVehicle.
Address object hierarchy Object Address HomeAddress CompanyAddress WorkAddress BoxAddress
Inheritance Class Name: Superclass: Subclasses: CompanyAddress Address BoxAddress Responsibilities Collaborations
Inheritance Superclass Derived class (subclass)
Inheritance Derived class
Inheritance Superclass Note constructors are not inherited
Inheritance All classes extend Object by default
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
Inheritance Hiding
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
Inheritance • public class Example • { • char letter; • public static String lineIs(); • … • } • public class ExtExample extends Example • { • char letter; • public static String lineIs(); • … • } Hiding or overriding?
Inheritance • public class Example • { • char letter; • public String lineIs(); • … • } • public class ExtExample extends Example • { • String letter; • public String lineIs(); • … • } Hiding or overriding?
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?"
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
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)
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)
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)
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
Inheritance • To what do each of these refer? • super(); • super(Scanner in); • someInt = super.someInt; • super.instanceMethod(); • super.classMethod(); • super.classMethod();
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
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?
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
Scope • External scope • Access to class members from code outside the class; controlled by access modifiers • Access modifiers • Remember them?
Scope • public • protected • ( no access modifier ) • “package access” • private public protected package private
Scope Public access
Scope Protected assess
Scope Package access within/between packages
Scope Private access
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
Scope • A final word… • A class marked final cannot be extended • A method marked final cannot be overridden or hidden
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
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(); … }
Polymorphism • The rest of the puzzle • Reference • array interface class
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!
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
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?
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?
Polymorphism • A useful operator: instanceof • if (myPhone instanceof BusinesPhone) • newPhone = in.inutbusinessPhone() • else • newPhone = in.inputPhone() • A useful method: getClass() • myPhone.getClass().getSimpleName()
Polymorphism • Two useful methods: • getClass() & getSimpleName() • if (myPhone.getClass().getSimpleName().equals("Phone")) instance returns returns name class of of class of instance instance
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?
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
Extras - GUI Track JFrame object with an input field