Chapter 8 More Object Concepts

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Introduction to Inheritance
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
Chapter 10 Classes Continued
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 12: Adding Functionality to Your Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object-Oriented Programming Chapter Chapter
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Understanding Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Java Programming, Second Edition
Inheritance.
Presentation transcript:

Chapter 8 More Object Concepts An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 8 More 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 An 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 An 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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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