Your SlideShare is downloading. ×
0
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Chapter 05 polymorphism extra
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Chapter 05 polymorphism extra

2,887

Published on

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,887
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
66
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 1
    Chapter 05: Polymorphism
  • 2. Overriding Superclass Methods
    2
    Create subclass by extending existing class
    Subclass contains data and methods defined in original superclass
    Sometimes superclass data fields and methods not entirely appropriate for subclass objects
    Polymorphism
    Using same method name to indicate different implementations
  • 3. Overriding Superclass Methods (continued)
    3
    Override methodin parent class
    Create method in child class that has same name and argument list as method in parent class
    Subtype polymorphism
    Ability of one method name to work appropriately for different subclass objects of same parent class
  • 4. Understanding How ConstructorsAre Called During Inheritance
    4
    Instantiate object that is member of subclass
    Call at least two constructors
    Constructor for base class
    Constructor for derived class
    Superclass constructor must execute first
    When superclass contains default constructor
    Execution of superclass constructor transparent
  • 5. 5
  • 6. Understanding How ConstructorsAre Called During Inheritance (continued)
    6
  • 7. Using Superclass Constructorsthat Require Arguments
    7
    When you write your own constructor
    You replace automatically supplied version
    When extending superclass with constructors that require arguments
    Subclass must provide superclass constructor with arguments it needs
  • 8. Using Superclass Constructorsthat Require Arguments (continued)
    8
    When superclass has default constructor
    Can create subclass with or without own constructor
    When superclass contains only constructors that require arguments
    Must include at least one constructor for each subclass you create
    First statement within each constructor must call superclass constructor
  • 9. Using Superclass Constructorsthat Require Arguments (continued)
    9
    Call superclass constructor
    super(list of arguments);
    Keyword super
    Always refers to superclass
  • 10. Accessing Superclass Methods
    10
    Use overridden superclass method within subclass
    Use keyword super to access parent class method
    Comparing this and super
    Think of the keyword this as the opposite of super
    Within a subclass
    When parent class contains a method that is not overridden
    Child can use the method name with super, this, or alone
  • 11. Accessing Superclass Methods (continued)
    11
  • 12. Quick Quiz
    12
    1. 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 Hiding
    13
    Student class
    Keyword private precedes each data field
    Keyword public precedes each method
    Information hiding
    Concept of keeping data private
    Data can be altered only by methods you choose and only in ways that you can control
  • 14. 14
  • 15. Learning About Information Hiding (continued)
    15
    When class serves as superclass
    Subclasses inherit all data and methods of superclass
    Except private members of parent class not accessible within child class’s methods
  • 16. Learning About Information Hiding (continued)
    16
    Keyword protected
    Provides intermediate level of security between public and private access
    Can be used within own class or in any classes extended from that class
    Cannot be used by “outside” classes
  • 17. Methods You Cannot Override
    17
    static methods
    final methods
    Methods within final classes
  • 18. A Subclass Cannot Override staticMethods in Its Superclass
    18
    Subclass cannot override methods declared static in superclass
    Can hidestatic method in superclass
    By declaring static method with same signature as static method in superclass
    Call new static method from within subclass or in another class by using subclass object
    Within static method of subclass
    Cannot access parent method using super object
  • 19. A Subclass Cannot Override staticMethods in Its Superclass (continued)
    19
    Although child class cannot inherit parent’s static methods
    Can access parent’s 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 Superclass
    21
    Subclass cannot override methods declared final in superclass
    final modifier
    Does not allow method to be overridden
    Virtual method calls
    Default in Java
    Method 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)
    22
    Advantage to making method final
    Compiler knows there is only one version of method
    Compiler knows which method version will be used
    Can optimize program’s performance
    By removing calls to final methods
    Replacing them with expanded code of their definitions
    At each method call location
    Called inlining
  • 23. A Subclass Cannot Override Methodsin a final Superclass
    23
    Declare class final
    All of its methods are final
    Regardless of which access modifier precedes method name
    Cannot be a parent class
  • 24. A Subclass Cannot Override Methodsin a final Superclass (continued)
    24
  • 25. You Do It
    25
    Creating a superclass and an application to use it
    Creating a subclass and an application to use it
    Creating a subclass method that overrides a superclass method
    Understanding the role of constructors in inheritance
  • 26. You Do It (continued)
    26
    Inheritance when the superclass requires constructor arguments
    Accessing an overridden superclass method from within a subclass
  • 27. Don’t Do It
    27
    Don’t capitalize the “o” in the instanceof operator
    Don’t try to directly access private superclass members from a subclass
    Don’t forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor
  • 28. Summary
    28
    Inheritance
    Mechanism that enables one class to inherit both behavior and attributes of another class
    Keyword extends
    Achieve inheritance in Java
    Polymorphism
    Act of using same method name to indicate different implementations
  • 29. Summary (continued)
    29
    Use a superclass method within a subclass
    Use keyword super to access it
    Information hiding
    Concept of keeping data private
    Keyword protected
    Intermediate level of security between public and private access
    Subclass cannot override methods
    Declared static in superclass
    Declared final or class final
  • 30. 30
    Quick 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 parent’s static methods, it can access its parent’s 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.

×