Object-Oriented Programming: Inheritance

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 9 Polymorphism Richard Gesick.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Spring CISC6795: Object Oriented Programming II.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Object Oriented Programming
Object Oriented Programming: Inheritance Chapter 9.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance and Polymorphism
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Programming: Inheritance
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Lecture 6 Inheritance CSE /26/2018.
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

Object-Oriented Programming: Inheritance 10 Object-Oriented Programming: Inheritance

Good as it is to inherit a library, it is better to collect one. Say not you know another entirely, till you have divided a inheritance with him. Johann Kasper Lavater This method is to define as the number of a class the class of all classes similar to the given class. Bertrand Russell Good as it is to inherit a library, it is better to collect one. Augustine Birrell Save base authority from others’ books. William Shakespeare

OBJECTIVES In this chapter you will learn: What inheritance is and how it promotes software reusability. The notions of base classes and derived classes. To use keyword Inherits to create a class that inherits attributes and behaviors from another class. To use the access modifier Protected in a base class to give derived class methods access to base class members. To access base class members from a derived class with MyBase. How constructors are used in inheritance hierarchies. To access the current object with Me and MyClass. The methods of class Object—the direct or indirect base class of all classes in Visual Basic.

10.1 Introduction 10.2 Base Classes and Derived Classes 10.3 Protected Members 10.4 Relationship between Base Classes and Derived Classes 10.4.1 Creating and Using a CommissionEmployee Class 10.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance 10.4.3 Creating a CommissionEmployee– BasePlusCommission Employee Inheritance Hierarchy 10.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using Protected Instance Variables 10.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using Private Instance Variables

10.5 Constructors in Derived Classes 10.6 Software Engineering with Inheritance 10.7 Class Object 10.8 Friend Members 10.9 Wrap-Up

10.1 Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Derived class extends base class Derived class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors

10.1 Introduction (Cont.) Class hierarchy Direct base class Inherited explicitly (one level up hierarchy) Indirect base class Inherited two or more levels up hierarchy Single inheritance Inherits from one base class Multiple inheritance Inherits from multiple base classes Visual Basic does not support multiple inheritance

10.2 Base Classes and Derived Classes Object of one class “is an” object of another class Example: Rectangle is a quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral: base class Rectangle: derived class Base class typically represents larger set of objects than derived classes Example: Base class: Vehicle Cars, trucks, boats, bicycles, … Derived class: Car Smaller, more-specific subset of vehicles

Fig. 10.1 | Inheritance examples.

10.2 Base Classes and Derived Classes (Cont.) Inheritance hierarchy Inheritance relationships: tree-like hierarchy structure Each class becomes Base class Supply members to other classes OR Derived class Inherit members from other classes

Fig. 10.2 | Inheritance hierarchy for university CommunityMembers.

Fig. 10.3 | Inheritance hierarchy for Shapes.

10.3 Protected Members Protected access Intermediate level of protection between Public and Private Protected members accessible by Base class members Derived class members Derived class access to base class member Keyword MyBase and a dot (.)

Software Engineering Observation 10.1 Derived class methods cannot directly access Private members of their base class. A derived class can change the state of Private base class instance variables only through non-Private methods provided in the base class and inherited by the derived class.

Software Engineering Observation 10.2 Declaring Private instance variables helps programmers test, debug and correctly modify systems. If a derived class could access its base class’s Private instance variables, classes that inherit from that derived class could access the instance variables as well. This would propagate access to what should be Private instance variables, and the benefits of information hiding would be lost.

10.4 Relationship between Base Classes and Derived Classes Base class and derived class relationship Example: CommissionEmployee/BasePlusCommissionEmployee inheritance hierarchy CommissionEmployee First name, last name, SSN, commission rate, gross sale amount BasePlusCommissionEmployee Base salary

10.4.1 Creating and Using a CommissionEmployee Class Class CommissionEmployee Extends class Object Inherits Every class in Visual Basic extends an existing class Except object Every class inherits object’s methods New class implicitly extends object To override base class method use keyword Overrides with the same signature (Overridden method must be declared Overridable) Constructors are not inherited The first task of any derived class constructor is to call its direct base class’s constructor

Outline Class CommissionEmployee Inherits class Object Declare Private instance variables Commmission Employee.vb (1 of 4 ) Implicit call to object constructor Initialize instance variables Invoke properties GrossSales and CommissionRate to validate data

Outline Commmission Employee.vb (2 of 4 )

Outline Commmission Employee.vb (3 of 4 ) Validation

Outline Validation (4 of 4 ) Calculate earnings Commmission Employee.vb (4 of 4 ) Calculate earnings Override method ToString of class Object

Common Programming Error 10.1 It is a compilation error to attempt to override a method that is not declared Overridable.

Common Programming Error 10.2 It is a compilation error to override a method with a method that has a different access modifier than the method being overridden.

Outline Instantiate CommissionEmployee object (1 of 2 ) Commission EmployeeTest.vb (1 of 2 ) Use CommissionEmployee’s properties to retrieve and change the object’s instance variable values Explicit call the object’s ToString method

Outline Explicit call the object’s CalculateEarnings method (2 of 2 ) Commission EmployeeTest.vb (2 of 2 ) Explicit call the object’s CalculateEarnings method

10.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance Class BasePlusCommissionEmployee Much of the code is similar to CommissionEmployee Private instance variables Public methods Constructor Properties Additions Private instance variable baseSalaryValue BaseSalary property

Outline Add instance variable baseSalary (1 of 5 ) BasePlus Commission Employee.vb (1 of 5 ) Use property BaseSalary to validate data

Outline BasePlus Commission Employee.vb (2 of 5 )

Outline BasePlus Commission Employee.vb (3 of 5 )

Outline Validates data and sets instance variable baseSalaryValue BasePlus Commission Employee.vb (4 of 5 ) Update method CalculateEarnings to calculate the earnings of a base-salaried commission employee

Outline (5 of 5 ) Update method ToString to display base salary BasePlus Commission Employee.vb (5 of 5 ) Update method ToString to display base salary

Outline Instantiate BasePlusCommissionEmployee object (1 of 2 ) BasePlus Commission EmployeeTest.vb (1 of 2 ) Use BasePluCommissionEmployee’s properties to retrieve and change the object’s instance variable values Explicitly call the object’s ToString method

Outline (2 of 2 ) Call the object’s CalculateEarnings method BasePlus Commission EmployeeTest.vb (2 of 2 )

Software Engineering Observation 10.3 Copying and pasting code from one class to another can spread errors among multiple source code files. To avoid duplicating code (and possibly errors), use inheritance, rather than the “copy-and-paste” approach, where you want one class to “absorb” the members of another class.

Software Engineering Observation 10.4 With inheritance, the common instance variables and methods of all the classes in the hierarchy are declared in a base class. When changes are required for these common features, software developers need to make the changes only in the base class—derived classes then inherit the changes. Without inheritance, the changes would need to be made to all the source code files that contain copies of the code in question.

10.4.3 Creating a CommissionEmployee-BasePlusCommiionEmployee Inheritance Hierarchy Class BasePlusCommissionEmployee Inherits class CommissionEmployee Is a CommissionEmployee Has instance variable baseSalaryValue Inherits Public and Protected members Constructor not inherited

Outline Commmission Employee.vb (1 of 4 )

Outline Commmission Employee.vb (2 of 4 )

Outline Commmission Employee.vb (3 of 4 )

Outline Commmission Employee.vb (4 of 4 ) Derived classes can now override the CalculateEarnings method

Outline Class BasePluCommissionEmployee is a derived class of CommissionEmployee BasePlus Commission Employee.vb (1 of 3 ) Invoke the base class constructor using the base class constructor call syntax

Outline BasePlus Commission Employee.vb (2 of 3 ) Compiler generates errors because base class’s instance variable are Private

Outline BasePlus Commission Employee.vb (3 of 3 )

10.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using Protected Instance Variables Use protected instance variables Enable class BasePlusCommissionEmployee to directly access base class instance variables Base class’s Protected members are inherited by all derived classes of that base class

Outline Declare Protected instance variables (1 of 4 ) Commmission Employee.vb (1 of 4 )

Outline Commmission Employee.vb (2 of 4 )

Outline Commmission Employee.vb (3 of 4 )

Outline Commmission Employee.vb (4 of 4 )

Outline (1 of 2 ) Must call base class’s constructor BasePlus Commission Employee.vb (1 of 2 ) Must call base class’s constructor

Outline (2 of 2 ) Overrides base class’s CalculateEarnings method BasePlus Commission Employee.vb (2 of 2 ) Overrides base class’s CalculateEarnings method Directly access base class’s Protected instance variables

Outline Instantiate BasePlusCommissionEmployee object (1 of 2 ) BasePlus Commission EmployeeTest.vb (1 of 2 ) Use BasePluCommissionEmployee’s properties to retrieve and change the object’s instance variable values Explicitly call the object’s overridden ToString method

Outline BasePlus Commission EmployeeTest.vb (2 of 2 )

10.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using Protected Instance Variables (Cont.) Using Protected instance variables Advantages Derived classes can modify values directly Slight increase in performance Avoid Set/Get accessors call overhead Disadvantages No validity checking Derived class can assign illegal value Implementation dependent Derived class methods more likely dependent on base class implementation Derived class implementation changes may result in derived class modifications Fragile (brittle) software

Software Engineering Observation 10.5 Use the Protected access modifier on a method when a base class is to provide the method to its derived classes but not to other clients.

10.4.5 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using Private Instance Variables Reexamine hierarchy Use the best software engineering practice Declare instance variables as Private Provide Public Get and Set accessors Use Get accessor to obtain values of instance variables

Software Engineering Observation 10.6 Declaring base class instance variables Private (as opposed to Protected) enables the base class implementation of these instance variables to change without affecting derived class implementations.

Error-Prevention Tip 10.1 When possible, do not include Protected instance variables in a base class. Instead, include non-Private properties and methods that carefully access Private instance variables. This will ensure that objects of the derived classes of this base class maintain consistent states of the base class instance variables.

Outline Declare Private instance variables (1 of 4 ) Commmission Employee.vb (1 of 4 )

Outline Commmission Employee.vb (2 of 4 )

Outline Commmission Employee.vb (3 of 4 )

Outline Commmission Employee.vb (4 of 4 ) Use properties to obtain the values of instance variables

Outline Inherits from CommissionEmployee (1 of 2 ) BasePlus Commission Employee.vb (1 of 2 )

Outline BasePlus Commission Employee.vb (2 of 2 ) Invoke an overridden base class method from a derived class Use properties to obtain the values of instance variables Invoke an overridden base class method from a derived class

Common Programming Error 10.3 When a base class method is overridden in a derived class, the derived class version often calls the base class version to do a portion of the work. Failure to prefix the base class method name with the keyword MyBase and a dot (.) separator when referencing the base class’s method causes the derived class method to call itself, creating an error called infinite recursion.

Outline Create BasePlusCommissionEmployee object. (1 of 2 ) BasePlus Commission EmployeeTest.vb (1 of 2 ) Use inherited properties to access inherited Private instance variables

Outline BasePlus Commission EmployeeTest.vb (2 of 2 )

10.5 Constructors in Derived Classes Instantiating derived class object Chain of constructor calls Derived class constructor invokes base class constructor Implicitly or explicitly Base of inheritance hierarchy Last constructor called in chain is Object’s constructor Original derived class constructor’s body finishes executing last Example: CommissionEmployee-BasePlusCommissionEmployee hierarchy CommissionEmployee constructor called second last (last is Object constructor) CommissionEmployee constructor’s body finishes execution second (first is Object constructor’s body) My Class keyword Similar to Me Always invoke the version of the method defined in that particular class Cannot be used to reference Shared class methods They can exist when there are no objects of that class

Outline Commission Employee.vb (1 of 4 ) Constructor outputs message to demonstrate method call order.

Outline Commission Employee.vb (2 of 4 )

Outline Commission Employee.vb (3 of 4 )

Outline Commission Employee.vb (4 of 4 )

Outline BasePlus Commission Employee.vb (1 of 2 ) Constructor outputs message to demonstrate method call order.

Outline BasePlus Commission Employee.vb (2 of 2 )

Outline Instantiate CommissionEmployee object Constructor.vb Instantiate BasePlusCommissionEmployee object to demonstrate order of derived class and base class constructor method calls. Uses the Me reference

Outline Uses the MyClass reference

10.6 Software Engineering with Inheritance Customizing existing software Inherit from existing classes Include additional members Redefine base class members No direct access to base class’s source code Link to object code Independent software vendors (ISVs) Develop proprietary code for sale/license Available in object-code format Users derive new classes Without accessing ISV proprietary source code

Software Engineering Observation 10.7 Despite the fact that inheriting from a class does not require access to the class’s source code, developers often insist on seeing the source code to see how the class is implemented. They want to ensure that they are extending a solid class that performs well and is implemented securely.

Software Engineering Observation 10.8 At the design stage in an object-oriented system, the designer often finds that certain classes are closely related. The designer should “factor out” common instance variables and methods and place them in a base class. Then the designer should use inheritance to develop derived classes, specializing them with capabilities beyond those inherited from the base class.

Performance Tip 10.1 If derived classes are larger than they need to be (i.e., contain too much functionality), memory and processing resources may be wasted. Extend the base class that contains the functionality that is closest to what you need.

10.7 Class Object Class Object methods Equals Finalize GetHashCode GetType MemberwiseClone ReferenceEquals ToString

Fig. 10. 20 | Object methods that are inherited by all classes Fig. 10.20 | Object methods that are inherited by all classes. (Part 1 of 3.)

Fig. 10. 20 | Object methods that are inherited by all classes Fig. 10.20 | Object methods that are inherited by all classes. (Part 2 of 3.)

Fig. 10. 20 | Object methods that are inherited by all classes Fig. 10.20 | Object methods that are inherited by all classes. (Part 3 of 3.)

10.8 Friend Members Friend Access Another intermediate level Can be accessed only by code in the same assembly No “special” effect where there is only one class declaration To access a non-Shared Friend member: nameOfObject.nonSharedFriendMember To access a Shared Friend member: nameOfClass. SharedFriendMember Protected Friend Members Are accessible both from code in the same assembly and by derived classes