chapter 05 polymorphism extra

30
1 Chapter 05: Polymorphism

Upload: nurhanna-aziz

Post on 11-Nov-2014

3.007 views

Category:

Technology


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Chapter 05 polymorphism extra

1

Chapter 05: Polymorphism

Page 2: Chapter 05 polymorphism extra

Overriding Superclass Methods2

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 objectsPolymorphism

Using same method name to indicate different implementations

Page 3: Chapter 05 polymorphism extra

Overriding Superclass Methods (continued)3

Override method in parent class Create method in child class that has same name and

argument list as method in parent classSubtype polymorphism

Ability of one method name to work appropriately for different subclass objects of same parent class

Page 4: Chapter 05 polymorphism extra

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 firstWhen superclass contains default constructor

Execution of superclass constructor transparent

Page 5: Chapter 05 polymorphism extra

5

Page 6: Chapter 05 polymorphism extra

Understanding How ConstructorsAre Called During Inheritance (continued)

6

Page 7: Chapter 05 polymorphism extra

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

Page 8: Chapter 05 polymorphism extra

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

Page 9: Chapter 05 polymorphism extra

Using Superclass Constructorsthat Require Arguments (continued)

9

Call superclass constructor super(list of arguments);

Keyword super Always refers to superclass

Page 10: Chapter 05 polymorphism extra

Accessing Superclass Methods10

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

Page 11: Chapter 05 polymorphism extra

Accessing Superclass Methods (continued)11

Page 12: Chapter 05 polymorphism extra

12

Quick Quiz

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.

Page 13: Chapter 05 polymorphism extra

Learning About Information Hiding13

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

Page 14: Chapter 05 polymorphism extra

14

Page 15: Chapter 05 polymorphism extra

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

Page 16: Chapter 05 polymorphism extra

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

Page 17: Chapter 05 polymorphism extra

Methods You Cannot Override17

static methodsfinal methodsMethods within final classes

Page 18: Chapter 05 polymorphism extra

A Subclass Cannot Override staticMethods in Its Superclass

18

Subclass cannot override methods declared static in superclass

Can hide static 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

Page 19: Chapter 05 polymorphism extra

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

Page 20: Chapter 05 polymorphism extra

A Subclass Cannot Override staticMethods in Its Superclass (continued)

20

Page 21: Chapter 05 polymorphism extra

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

Page 22: Chapter 05 polymorphism extra

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

Page 23: Chapter 05 polymorphism extra

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

Page 24: Chapter 05 polymorphism extra

A Subclass Cannot Override Methodsin a final Superclass (continued)

24

Page 25: Chapter 05 polymorphism extra

You Do It25

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

Page 26: Chapter 05 polymorphism extra

You Do It (continued)26

Inheritance when the superclass requires constructor arguments

Accessing an overridden superclass method from within a subclass

Page 27: Chapter 05 polymorphism extra

Don’t Do It27

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

Page 28: Chapter 05 polymorphism extra

Summary28

Inheritance Mechanism that enables one class to inherit both

behavior and attributes of another classKeyword extends

Achieve inheritance in JavaPolymorphism

Act of using same method name to indicate different implementations

Page 29: Chapter 05 polymorphism extra

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

Page 30: Chapter 05 polymorphism extra

30Quick Quiz 3 1. 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.