Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 More Object Concepts

Similar presentations


Presentation on theme: "Chapter 8 More Object Concepts"— Presentation transcript:

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

2 Objectives In this chapter, you will learn about: Constructors
Destructors The concept of inheritance Inheritance terminology Accessing private members of a parent class An Object-Oriented Approach to Programming Logic and Design

3 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 An Object-Oriented Approach to Programming Logic and Design

4 An Introduction to Constructors
Can create classes to encapsulate data and methods Can instantiate objects from these classes Figure 8-1 Figure 8-1 shows an Employee class Contains the fields lastName and hourlyWage Contains methods that set and return values for those fields An Object-Oriented Approach to Programming Logic and Design

5 An Introduction to Constructors (cont’d)
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 An Object-Oriented Approach to Programming Logic and Design

6 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 An Object-Oriented Approach to Programming Logic and Design

7 A default constructor has no parameters
Figure 8-2 Employee class with a default constructor that sets a value for hourlyWage A default constructor has no parameters Constructor calls setHourlyWage() method and passes it 10.00 An Object-Oriented Approach to Programming Logic and Design

8 Program that declares Employee objects using class in Figure 8-2
setHourlyWage()not used in the program Figure 8-3 Figure 8-4 Sample program output for Figure 8-3 Wage: set by constructor An Object-Oriented Approach to Programming Logic and Design

9 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 Figure 8-5 An Object-Oriented Approach to Programming Logic and Design

10 Overloading Instance Methods and Constructors
Same concept as overloading methods Figure 8-6: Overloaded Employee class constructors Figure 8-6 One version requires no argument (default constructor) Other requires numeric argument An Object-Oriented Approach to Programming Logic and Design

11 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 Figure 8-7 An Object-Oriented Approach to Programming Logic and Design

12 Understanding Destructors
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 An Object-Oriented Approach to Programming Logic and Design

13 Program never explicitly calls Student class destructor
Student class with destructor Unusual for a constructor or destructor to display anything Figure 8-8 Figure 8-9 Program never explicitly calls Student class destructor Invoked automatically Figure 8-10 DemoStudentDestructor program output: first object created is last object destroyed An Object-Oriented Approach to Programming Logic and Design

14 Understanding 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 An Object-Oriented Approach to Programming Logic and Design

15 Understanding Inheritance (cont’d)
Figure 8-11 Figure 8-11 shows Customer class contains two data fields contains methods that get and set each field Sample Customer class objects Customer firstCustomer Customer SecondCustomer An Object-Oriented Approach to Programming Logic and Design

16 Figure 8-13 shows pseudocode for PreferredCustomer class
InheritsFrom Customer indicates inheritance Each programming language uses its own syntax Java: extends Figure 8-12 shows diagram of how PreferredCustomer class inherits from Customer class An Object-Oriented Approach to Programming Logic and Design

17 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 An Object-Oriented Approach to Programming Logic and Design

18 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 An Object-Oriented Approach to Programming Logic and Design

19 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 An Object-Oriented Approach to Programming Logic and Design

20 Understanding Inheritance Terminology (cont’d)
Possible ancestors and descendents of Dog class Figure 8-14 An Object-Oriented Approach to Programming Logic and Design

21 Understanding Inheritance Terminology (cont’d)
customer2 object can use all methods of its parent Figure 8-15 Figure 8-16 An Object-Oriented Approach to Programming Logic and Design

22 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 An Object-Oriented Approach to Programming Logic and Design

23 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 Figure 8-17 An Object-Oriented Approach to Programming Logic and Design

24 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 Figure 8-18 An Object-Oriented Approach to Programming Logic and Design

25 Customer class uses the protected access specifier on its purchaseTotal field
Figure 8-19 An Object-Oriented Approach to Programming Logic and Design

26 PreferredCustomer class when purchaseTotal remains private
Figure 8-20 PreferredCustomer class when purchaseTotal remains private Class uses the public method setPurchaseTotal() An Object-Oriented Approach to Programming Logic and Design

27 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 An Object-Oriented Approach to Programming Logic and Design

28 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 An Object-Oriented Approach to Programming Logic and Design

29 Overriding Base Class Methods (cont’d)
Figure 8-21 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 An Object-Oriented Approach to Programming Logic and Design

30 Figure 8-22 shows how Student class is implemented
An Object-Oriented Approach to Programming Logic and Design

31 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 An Object-Oriented Approach to Programming Logic and Design

32 Overriding Base Class Methods (cont’d)
Calls superclass version of the method setCredits() Figure 8-23 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 An Object-Oriented Approach to Programming Logic and Design

33 Overriding Base Class Methods (cont’d)
Figure 8-24 Application that declares a full-time Student and a full-time ScholarshipStudent Figure 8-25 An Object-Oriented Approach to Programming Logic and Design

34 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 An Object-Oriented Approach to Programming Logic and Design

35 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 An Object-Oriented Approach to Programming Logic and Design

36 Employee class contains single constructor that requires two parameters
Figure 8-26 Child class constructor calling base class constructor with constant arguments An Object-Oriented Approach to Programming Logic and Design

37 Employee class contains single constructor that requires two parameters
Child class constructor calling base class constructor with variable arguments Figure 8-27 An Object-Oriented Approach to Programming Logic and Design

38 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 An Object-Oriented Approach to Programming Logic and Design

39 Program passes a Student object and a ScholarshipStudent object to the display()method
Figure 8-28 An Object-Oriented Approach to Programming Logic and Design

40 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 An Object-Oriented Approach to Programming Logic and Design

41 Summary Constructor Destructor 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 An Object-Oriented Approach to Programming Logic and Design

42 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 An Object-Oriented Approach to Programming Logic and Design

43 Summary (cont’d) Relationship terminology: Private data fields
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 An Object-Oriented Approach to Programming Logic and Design

44 Summary (cont’d) Derived class objects Use inheritance to:
“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 An Object-Oriented Approach to Programming Logic and Design


Download ppt "Chapter 8 More Object Concepts"

Similar presentations


Ads by Google