1<br />Chapter 05: Polymorphism<br />
Overriding Superclass Methods<br />2<br />Create subclass by extending existing class<br />Subclass contains data and meth...
Overriding Superclass Methods (continued)<br />3<br />Override methodin parent class<br />Create method in child class tha...
Understanding How ConstructorsAre Called During Inheritance<br />4<br />Instantiate object that is member of subclass <br ...
5<br />
Understanding How ConstructorsAre Called During Inheritance (continued)<br />6<br />
Using Superclass Constructorsthat Require Arguments<br />7<br />When you write your own constructor<br />You replace autom...
Using Superclass Constructorsthat Require Arguments (continued)<br />8<br />When superclass has default constructor<br />C...
Using Superclass Constructorsthat Require Arguments (continued)<br />9<br />Call superclass constructor<br />super(list of...
Accessing Superclass Methods<br />10<br />Use overridden superclass method within subclass<br />Use keyword super to acces...
Accessing Superclass Methods (continued)<br />11<br />
Quick Quiz<br />12<br />1.	Using the same method name to indicate different implementations is called ____. <br />2. True ...
Learning About Information Hiding<br />13<br />Student class<br />Keyword private precedes each data field<br />Keyword pu...
14<br />
Learning About Information Hiding (continued)<br />15<br />When class serves as superclass <br />Subclasses inherit all da...
Learning About Information Hiding (continued)<br />16<br />Keyword protected<br />Provides intermediate level of security ...
Methods You Cannot Override<br />17<br />static methods<br />final methods<br />Methods within final classes<br />
A Subclass Cannot Override staticMethods in Its Superclass<br />18<br />Subclass cannot override methods declared static i...
A Subclass Cannot Override staticMethods in Its Superclass (continued)<br />19<br />Although child class cannot inherit pa...
A Subclass Cannot Override staticMethods in Its Superclass (continued)<br />20<br />
A Subclass Cannot Override finalMethods in Its Superclass<br />21<br />Subclass cannot override methods declared final in ...
A Subclass Cannot Override finalMethods in Its Superclass (continued)<br />22<br />Advantage to making method final<br />C...
A Subclass Cannot Override Methodsin a final Superclass<br />23<br />Declare class final<br />All of its methods are final...
A Subclass Cannot Override Methodsin a final Superclass (continued)<br />24<br />
You Do It<br />25<br />Creating a superclass and an application to use it<br />Creating a subclass and an application to u...
You Do It (continued)<br />26<br />Inheritance when the superclass requires constructor arguments<br />Accessing an overri...
Don’t Do It<br />27<br />Don’t capitalize the “o” in the instanceof operator<br />Don’t try to directly access private sup...
Summary<br />28<br />Inheritance<br />Mechanism that enables one class to inherit both behavior and attributes of another ...
Summary (continued)<br />29<br />Use a superclass method within a subclass<br />Use keyword super to access it<br />Inform...
30<br />Quick Quiz 3 <br />The concept of keeping data private is known as ____. <br />2. Using the keyword ____ provides ...
Upcoming SlideShare
Loading in …5
×

Chapter 05 polymorphism extra

3,855 views

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
3,855
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
70
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 05 polymorphism extra

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

×