際際滷

際際滷Share a Scribd company logo
1Chapter 05: Polymorphism
Overriding Superclass Methods2Create subclass by extending existing classSubclass contains data and methods defined in original superclassSometimes superclass data fields and methods not entirely appropriate for subclass objectsPolymorphismUsing same method name to indicate different implementations
Overriding Superclass Methods (continued)3Override methodin parent classCreate method in child class that has same name and argument list as method in parent classSubtype polymorphismAbility of one method name to work appropriately for different subclass objects of same parent class
Understanding How ConstructorsAre Called During Inheritance4Instantiate object that is member of subclass Call at least two constructorsConstructor for base class Constructor for derived classSuperclass constructor must execute firstWhen superclass contains default constructorExecution of superclass constructor transparent
5
Understanding How ConstructorsAre Called During Inheritance (continued)6
Using Superclass Constructorsthat Require Arguments7When you write your own constructorYou replace automatically supplied versionWhen extending superclass with constructors that require argumentsSubclass must provide superclass constructor with arguments it needs
Using Superclass Constructorsthat Require Arguments (continued)8When superclass has default constructorCan create subclass with or without own constructorWhen superclass contains only constructors that require argumentsMust include at least one constructor for each subclass you createFirst statement within each constructor must call superclass constructor
Using Superclass Constructorsthat Require Arguments (continued)9Call superclass constructorsuper(list of arguments);Keyword superAlways refers to superclass
Accessing Superclass Methods10Use overridden superclass method within subclassUse keyword super to access parent class methodComparing this and superThink of the keyword this as the opposite of super Within a subclassWhen parent class contains a method that is not overriddenChild can use the method name with super, this, or alone
Accessing Superclass Methods (continued)11
Quick Quiz121.	Using the same method name to indicate different implementations is called ____. 2. True or False: When you instantiate an object that is a member of a subclass, you are actually calling at least two constructors. 3. The keyword ____ always refers to the superclass of the class in which you use it.
Learning About Information Hiding13Student classKeyword private precedes each data fieldKeyword public precedes each methodInformation hidingConcept of keeping data privateData can be altered only by methods you choose and only in ways that you can control
14
Learning About Information Hiding (continued)15When class serves as superclass Subclasses inherit all data and methods of superclassExcept private members of parent class not accessible within child classs methods
Learning About Information Hiding (continued)16Keyword protectedProvides intermediate level of security between public and private accessCan be used within own class or in any classes extended from that classCannot be used by outside classes
Methods You Cannot Override17static methodsfinal methodsMethods within final classes
A Subclass Cannot Override staticMethods in Its Superclass18Subclass cannot override methods declared static in superclassCan hidestatic method in superclassBy declaring static method with same signature as static method in superclassCall new static method from within subclass or in another class by using subclass objectWithin static method of subclassCannot access parent method using super object
A Subclass Cannot Override staticMethods in Its Superclass (continued)19Although child class cannot inherit parents static methodsCan access parents static methods in the same way any other class can
A Subclass Cannot Override staticMethods in Its Superclass (continued)20
A Subclass Cannot Override finalMethods in Its Superclass21Subclass cannot override methods declared final in superclassfinal modifierDoes not allow method to be overriddenVirtual method callsDefault in JavaMethod used is determined when program runs Type of object used might not be known until method executes
A Subclass Cannot Override finalMethods in Its Superclass (continued)22Advantage to making method finalCompiler knows there is only one version of methodCompiler knows which method version will be usedCan optimize programs performance By removing calls to final methods Replacing them with expanded code of their definitions At each method call locationCalled inlining
A Subclass Cannot Override Methodsin a final Superclass23Declare class finalAll of its methods are finalRegardless of which access modifier precedes method nameCannot be a parent class
A Subclass Cannot Override Methodsin a final Superclass (continued)24
You Do It25Creating a superclass and an application to use itCreating a subclass and an application to use itCreating a subclass method that overrides a superclass methodUnderstanding the role of constructors in inheritance
You Do It (continued)26Inheritance when the superclass requires constructor argumentsAccessing an overridden superclass method from within a subclass
Dont Do It27Dont capitalize the o in the instanceof operatorDont try to directly access private superclass members from a subclassDont forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor
Summary28InheritanceMechanism that enables one class to inherit both behavior and attributes of another classKeyword extendsAchieve inheritance in JavaPolymorphism Act of using same method name to indicate different implementations
Summary (continued)29Use a superclass method within a subclassUse keyword super to access itInformation hidingConcept of keeping data privateKeyword protectedIntermediate level of security between public and private accessSubclass cannot override methods Declared static in superclassDeclared final or class final
30Quick Quiz 3 The concept of keeping data private is known as ____. 2. Using the keyword ____ provides you with an intermediate level of security between public and private access. 3. True or False: Although a child class cannot inherit its parents static methods, it can access its parents static methods the same way any other class can. 4. You can declare a class to be ____. When you do, all of its methods are final, regardless of which access modifier precedes the method name.

More Related Content

Chapter 05 polymorphism extra

  • 2. Overriding Superclass Methods2Create subclass by extending existing classSubclass contains data and methods defined in original superclassSometimes superclass data fields and methods not entirely appropriate for subclass objectsPolymorphismUsing same method name to indicate different implementations
  • 3. Overriding Superclass Methods (continued)3Override methodin parent classCreate method in child class that has same name and argument list as method in parent classSubtype polymorphismAbility of one method name to work appropriately for different subclass objects of same parent class
  • 4. Understanding How ConstructorsAre Called During Inheritance4Instantiate object that is member of subclass Call at least two constructorsConstructor for base class Constructor for derived classSuperclass constructor must execute firstWhen superclass contains default constructorExecution of superclass constructor transparent
  • 5. 5
  • 6. Understanding How ConstructorsAre Called During Inheritance (continued)6
  • 7. Using Superclass Constructorsthat Require Arguments7When you write your own constructorYou replace automatically supplied versionWhen extending superclass with constructors that require argumentsSubclass must provide superclass constructor with arguments it needs
  • 8. Using Superclass Constructorsthat Require Arguments (continued)8When superclass has default constructorCan create subclass with or without own constructorWhen superclass contains only constructors that require argumentsMust include at least one constructor for each subclass you createFirst statement within each constructor must call superclass constructor
  • 9. Using Superclass Constructorsthat Require Arguments (continued)9Call superclass constructorsuper(list of arguments);Keyword superAlways refers to superclass
  • 10. Accessing Superclass Methods10Use overridden superclass method within subclassUse keyword super to access parent class methodComparing this and superThink of the keyword this as the opposite of super Within a subclassWhen parent class contains a method that is not overriddenChild can use the method name with super, this, or alone
  • 12. Quick Quiz121. Using the same method name to indicate different implementations is called ____. 2. True or False: When you instantiate an object that is a member of a subclass, you are actually calling at least two constructors. 3. The keyword ____ always refers to the superclass of the class in which you use it.
  • 13. Learning About Information Hiding13Student classKeyword private precedes each data fieldKeyword public precedes each methodInformation hidingConcept of keeping data privateData can be altered only by methods you choose and only in ways that you can control
  • 14. 14
  • 15. Learning About Information Hiding (continued)15When class serves as superclass Subclasses inherit all data and methods of superclassExcept private members of parent class not accessible within child classs methods
  • 16. Learning About Information Hiding (continued)16Keyword protectedProvides intermediate level of security between public and private accessCan be used within own class or in any classes extended from that classCannot be used by outside classes
  • 17. Methods You Cannot Override17static methodsfinal methodsMethods within final classes
  • 18. A Subclass Cannot Override staticMethods in Its Superclass18Subclass cannot override methods declared static in superclassCan hidestatic method in superclassBy declaring static method with same signature as static method in superclassCall new static method from within subclass or in another class by using subclass objectWithin static method of subclassCannot access parent method using super object
  • 19. A Subclass Cannot Override staticMethods in Its Superclass (continued)19Although child class cannot inherit parents static methodsCan access parents static methods in the same way any other class can
  • 20. A Subclass Cannot Override staticMethods in Its Superclass (continued)20
  • 21. A Subclass Cannot Override finalMethods in Its Superclass21Subclass cannot override methods declared final in superclassfinal modifierDoes not allow method to be overriddenVirtual method callsDefault in JavaMethod used is determined when program runs Type of object used might not be known until method executes
  • 22. A Subclass Cannot Override finalMethods in Its Superclass (continued)22Advantage to making method finalCompiler knows there is only one version of methodCompiler knows which method version will be usedCan optimize programs performance By removing calls to final methods Replacing them with expanded code of their definitions At each method call locationCalled inlining
  • 23. A Subclass Cannot Override Methodsin a final Superclass23Declare class finalAll of its methods are finalRegardless of which access modifier precedes method nameCannot be a parent class
  • 24. A Subclass Cannot Override Methodsin a final Superclass (continued)24
  • 25. You Do It25Creating a superclass and an application to use itCreating a subclass and an application to use itCreating a subclass method that overrides a superclass methodUnderstanding the role of constructors in inheritance
  • 26. You Do It (continued)26Inheritance when the superclass requires constructor argumentsAccessing an overridden superclass method from within a subclass
  • 27. Dont Do It27Dont capitalize the o in the instanceof operatorDont try to directly access private superclass members from a subclassDont forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor
  • 28. Summary28InheritanceMechanism that enables one class to inherit both behavior and attributes of another classKeyword extendsAchieve inheritance in JavaPolymorphism Act of using same method name to indicate different implementations
  • 29. Summary (continued)29Use a superclass method within a subclassUse keyword super to access itInformation hidingConcept of keeping data privateKeyword protectedIntermediate level of security between public and private accessSubclass cannot override methods Declared static in superclassDeclared final or class final
  • 30. 30Quick Quiz 3 The concept of keeping data private is known as ____. 2. Using the keyword ____ provides you with an intermediate level of security between public and private access. 3. True or False: Although a child class cannot inherit its parents static methods, it can access its parents static methods the same way any other class can. 4. You can declare a class to be ____. When you do, all of its methods are final, regardless of which access modifier precedes the method name.