+ All Categories
Home > Documents > An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 8 More Object...

An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 8 More Object...

Date post: 27-Dec-2015
Category:
Upload: alisha-webster
View: 222 times
Download: 1 times
Share this document with a friend
Popular Tags:
44
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 8 More Object Concepts
Transcript

An Object-Oriented Approach to Programming Logic and Design

Fourth Edition

Chapter 8More Object Concepts

Objectives

In this chapter, you will learn about:• Constructors• Destructors• The concept of inheritance• Inheritance terminology• Accessing private members of a parent class

2An Object-Oriented Approach to Programming Logic and Design

Objectives (cont’d)

• Overriding base class methods• How constructors are called during inheritance• The concept that a derived class object “is an”

instance of the base class• Using inheritance to achieve good software design

3An Object-Oriented Approach to Programming Logic and Design

An Introduction to Constructors

• Can create classes to encapsulate data and methods• Can instantiate objects from these classes

4An Object-Oriented Approach to Programming Logic and Design

• Figure 8-1 shows an Employee class

• Contains the fields lastName and hourlyWage

• Contains methods that set and return values for those fields

Figure 8-1

An Introduction to Constructors (cont’d)

• Constructor– Method that establishes an object– Provides its name and reserves memory space– Created automatically by compiler for every class

• Declaring Employee object– Constructor establishes one Employee instance– Depending on language, may provide initial values for

object’s data fields

5An Object-Oriented Approach to Programming Logic and Design

An Introduction to Constructors (cont’d)

• Can write your own constructor– If you want different default values– If you want to perform additional tasks– Must have same name as class it constructs– Cannot have a return type– Place anywhere inside the class, outside another method

6An Object-Oriented Approach to Programming Logic and Design

Figure 8-2

7An Object-Oriented Approach to Programming Logic and Design

Employee class with a default constructor that sets a value for hourlyWage

Constructor calls setHourlyWage() method and passes it 10.00

A default constructor has no parameters

8An Object-Oriented Approach to Programming Logic and Design

Program that declares Employee objects using class in Figure 8-2

Sample program output for Figure 8-3

setHourlyWage()not used in the program

Wage: set by constructor

Figure 8-3

Figure 8-4

Constructors with Parameters• Constructors with parameters

– Allow each object instantiation to have different initial values

– Figure 8-5: Employee class with a constructor that accepts a parameter

– Must pass a numeric value to the constructor

9An Object-Oriented Approach to Programming Logic and Design

Figure 8-5

Overloading Instance Methods and Constructors

• Overloading instance methods and constructors– Same concept as overloading methods

• Figure 8-6: Overloaded Employee class constructors

10An Object-Oriented Approach to Programming Logic and Design

– One version requires no argument (default constructor)

– Other requires numeric argument Figure 8-6

Overloading Instance Methods and Constructors (cont’d)

• When using the class in Figure 8-6 (previous slide)– Can make the following statements

• Figure 8-7 shows overloaded constructors– Parameterless constructor calls one that requires a

parameter

11An Object-Oriented Approach to Programming Logic and Design

Figure 8-7

Understanding Destructors

• Destructor– Contains actions you require when instance of a class is

destroyed– Most often destroyed when it goes out of scope– Identifier (class name) is preceded by a tilde (~)– No arguments can be provided– Cannot be overloaded– No return type

12An Object-Oriented Approach to Programming Logic and Design

13An Object-Oriented Approach to Programming Logic and Design

Student class with destructor•Unusual for a constructor or destructor to display anything

• Program never explicitly calls Student class destructor– Invoked automatically

• DemoStudentDestructor program output: first object created is last object destroyed

Figure 8-8

Figure 8-9

Figure 8-10

Understanding Inheritance

• Inheritance– Introduced in Chapter 7– Can apply knowledge of a general category to more

specific objects– Classes can inherit data and methods from existing classes

14An Object-Oriented Approach to Programming Logic and Design

Figure 8-11

15An Object-Oriented Approach to Programming Logic and Design

Figure 8-11 shows Customer class•contains two data fields•contains methods that get and set each field

Sample Customer class objectsCustomer firstCustomerCustomer SecondCustomer

Understanding Inheritance (cont’d)

16An Object-Oriented Approach to Programming Logic and Design

Figure 8-12 shows diagram of how PreferredCustomer class inherits from Customer class

Figure 8-13 shows pseudocode for PreferredCustomer class •InheritsFrom Customer indicates inheritance•Each programming language uses its own syntax

– Java: extends

Figure 8-12

Figure 8-13

Understanding Inheritance (cont’d)• Benefits of using inheritance

– Saves time by not recreating Customer fields and methods

– Reduces chance of errors (Customer methods already used and tested)

– Makes it easier for anyone who used Customer class to understand PreferredCustomer class

– Reduces chance of inconsistencies in shared fields– Makes programs easier to write, easier to understand, and

less proven to errors– Makes code reusable

17An Object-Oriented Approach to Programming Logic and Design

Understanding Inheritance Terminology

• Base class– Class that is used as a basis for inheritance

• Derived class (or extended class)– Created class that inherits from a base class– “Is a” case or instance of the base class– PreferredCustomer “is a” Customer

• Superclass and subclass; parent class and child class– Synonyms for base class and derived class– Evergreen class can be considered a subclass of the Tree superclass

18An Object-Oriented Approach to Programming Logic and Design

Understanding Inheritance Terminology (cont’d)

• To discover which is base and which is derived class– Say names together; more specific name is first– Look at size; derived class generally larger than base

• Derived class can be further extended– A subclass can have a child of its own– Example: Evergreen (derived class from Tree class)

• Can create Spruce class from Evergreen

• Ancestors are an entire list of parent classes

19An Object-Oriented Approach to Programming Logic and Design

Understanding Inheritance Terminology (cont’d)

Possible ancestors and descendents of Dog class

20An Object-Oriented Approach to Programming Logic and Design

Figure 8-14

Understanding Inheritance Terminology (cont’d)

21An Object-Oriented Approach to Programming Logic and Design

customer2 object can use all methods of its parent

Figure 8-15

Figure 8-16

Understanding Inheritance Terminology (cont’d)

• A child class contains the data fields and methods of its parent

• Parent class object cannot access child’s data and methods

22An Object-Oriented Approach to Programming Logic and Design

Accessing Private Members of a Parent Class

• Methods are public, but data is often private– No outside class can access data directly– Child class cannot directly access parent class’s data

23An Object-Oriented Approach to Programming Logic and Design

Figure 8-17

Accessing Private Members of a Parent Class (cont’d)

• Protected access– Used when you want no outside classes except descendent

classes to use a data field

• Figure 8-18 shows how data and methods are accessible by child and outside classes

24An Object-Oriented Approach to Programming Logic and Design

Figure 8-18

25An Object-Oriented Approach to Programming Logic and Design

Customer class uses the protected access specifier on its purchaseTotal field

Figure 8-19

Figure 8-20

26An Object-Oriented Approach to Programming Logic and Design

PreferredCustomer class when purchaseTotal remains private

Class uses the public method setPurchaseTotal()

Accessing Private Members of a Parent Class (cont’d)

• Benefits of using public method to access private data– No protected access specifiers are needed in parent class– Creators of parent class did not have to foresee need for

protected field– If parent class method contains additional code to enforce

limits on values, they would be enforced for parent and child objects

– Likelihood of errors decreases

• Best approach is often to use public method

27An Object-Oriented Approach to Programming Logic and Design

Overriding Base Class Methods

• Override a method in a parent class– Create a method with same signature as parent version– Parent version becomes hidden from objects of the child

class– When using method name with child class object, child

class’s version is used– When using method name with parent class object, parent

class’s version is used

28An Object-Oriented Approach to Programming Logic and Design

Overriding Base Class Methods (cont’d)

29An Object-Oriented Approach to Programming Logic and Design

• Student class contains three fields and a get method for each field

• There is no set method for tuition because clients cannot set tuition directly

Figure 8-21

30An Object-Oriented Approach to Programming Logic and Design

• Figure 8-22 shows how Student class is implemented

Figure 8-22

Overriding Base Class Methods (cont’d)

• Three ways of creating ScholarshipStudent class – Create a special method in the ScholarshipStudent

class with a name such as setScholarshipStudentCredits()

– Create a method in the ScholarshipStudent class with same name as parent class method but different parameter list

– Override the setCredits()method in Student class• Best solution• Child class method is only used with child class objects

31An Object-Oriented Approach to Programming Logic and Design

Overriding Base Class Methods (cont’d)

32An Object-Oriented Approach to Programming Logic and Design

• Class contains setCredits() method – overrides setCredits()method in Student class

• actualCredits stores # of credits for which student is enrolled • Child class method setCredits()calls parent class method with

same name, passing 0 as the number of billableCredits

Calls superclass version of the method

setCredits()Figure 8-23

Figure 8-24

Overriding Base Class Methods (cont’d)

33An Object-Oriented Approach to Programming Logic and Design

Application that declares a full-time Student and a full-time ScholarshipStudent

Figure 8-25

Understanding How Constructors are Called during Inheritance

• Creating an object– Calling a constructor with the same name as the class

• Instantiating an object that is a member of a subclass– Calling two constructors: one for base class and one for

derived class– Superclass constructor executes first and then subclass

• If superclass has a default constructor– Can create a subclass with or without its own constructor

34An Object-Oriented Approach to Programming Logic and Design

Understanding How Constructors are Called during Inheritance (cont’d)

• If superclass has only constructors that require arguments– Subclass constructors can contain any number of

statements• Each subclass constructor must call superclass constructor

and pass required arguments• Parent class constructor must be fully executed before child

class constructor can operate– Must include at least one constructor for each subclass – Must call the superclass constructor and pass the required

arguments to it

35An Object-Oriented Approach to Programming Logic and Design

Figure 8-26

36An Object-Oriented Approach to Programming Logic and Design

Employee class contains single constructor that requires two parameters

Child class constructor calling base class constructor with constant arguments

37An Object-Oriented Approach to Programming Logic and Design

Employee class contains single constructor that requires two parameters

Child class constructor calling base class constructor with variable arguments

Figure 8-27

Understanding How a Derived Class Object “is an” Instance of the

Base Class• Every derived class object “is a” specific instance of

both the derived class and the base class– MyCar “is a” Car as well as a Vehicle

• Can assign a derived class object to an object of any type that is an ancestor

• Implicit conversion– Made when a derived class object is assigned to an object

of any of its superclass types

38An Object-Oriented Approach to Programming Logic and Design

Figure 8-28

39An Object-Oriented Approach to Programming Logic and Design

Program passes a Student object and a ScholarshipStudent object to the display()method

Using Inheritance to Achieve Good Software Design

• Benefits of using inheritance– Saves development time because much of class code is

already written– Saves testing time because superclass code has been

written and tested; it is reliable– Reduces time to learn the new class features– Superclass code maintains its integrity

40An Object-Oriented Approach to Programming Logic and Design

Summary

• Constructor– Method that establishes an object– Default constructor created automatically by the compiler– Can be created by the programmer– Must have the same name as the class it constructs and

cannot have return type– May receive arguments

• Destructor– Contains actions required when class instance is destroyed– Automatically provided unless explicitly created

41An Object-Oriented Approach to Programming Logic and Design

Summary (cont’d)

• Inheritance– Principle of applying knowledge of a general category to

specific objects– New class contains all fields and methods of an existing

class plus added members– Makes programs easier to write and understand– Less prone to errors

• Base class: class that is the basis for inheritance• Derived or extended class: class that inherits from a

base class

42An Object-Oriented Approach to Programming Logic and Design

Summary (cont’d)

• Relationship terminology: – parent and child class; super and subclass

• Private data fields – Child classes cannot access parent data fields– protected access modifier can be used– Can always use public methods of the base class

• Overriding methods in a child class– Create a method with same signature as parent version

• Instantiating subclass objects– Calls two constructors: one for base and one for derived class

43An Object-Oriented Approach to Programming Logic and Design

Summary (cont’d)

• Derived class objects– “Is a” specific instance of the derived class and base class

• Use inheritance to:– Reduce development and testing time– Provide reliable code– Make it easier for clients to learn to use the new class

44An Object-Oriented Approach to Programming Logic and Design


Recommended