Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Slides:



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

COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
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.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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)
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
 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.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting.
 2009 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Object Oriented Programming: Inheritance Chapter 9.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.

 This chapter continues our discussion of object-oriented programming by introducing inheritance.  It provides a method of software reuse in which a new class is created quickly and easily by absorbing an existing class’s members and customizing them with new or modified capabilities. © by Pearson Education, Inc. All Rights Reserved.

 When creating a class, rather than declaring completely new members, you can designate that the new class inherits the members of an existing class.  The existing class is called the base class, and the new class is the derived class.  A derived class can add its own instance variables, Shared variables, properties and methods, and it can customize methods and properties it inherits. © by Pearson Education, Inc. All Rights Reserved.

 We explain and demonstrate polymorphism, which enables you to conveniently program “in the general” rather than “in the specific.”  You’ll see that polymorphism simplifies programming with classes and makes it easy to extend systems with new capabilities. © by Pearson Education, Inc. All Rights Reserved.

 Inheritance enables an is-a relationship.  In an is-a relationship, an object of a derived class also can be treated as an object of its base class.  For example, a car is a vehicle.  Figure 10.1 lists several simple examples of base classes and derived classes—base classes tend to be more general and derived classes tend to be more specific.  Base-class objects cannot be treated as objects of their derived classes—although all cars are vehicles, not all vehicles are cars (the other vehicles could be trucks, planes or bicycles, for example) © by Pearson Education, Inc. All Rights Reserved.

Figure below lists several simple examples of base classes and derived classes—base classes tend to be more general and derived classes tend to be more specific.

© by Pearson Education, Inc. All Rights Reserved. Figure below shows a sample UML class diagram of an inheritance hierarchy.

 We use a business-oriented inheritance hierarchy containing types of employees in a company’s payroll application.  All employees of the company have a lot in common ◦ Base Class: Commission employees (who will be represented as objects of a base class) are paid a percentage of their sales, ◦ Derived Class: Base-salaried commission employees (who will be represented as objects of a derived class) receive a percentage of their sales plus a base salary. © by Pearson Education, Inc. All Rights Reserved.

 First, we present base class CommissionEmployee.  Next, we create a derived class BasePlusCommissionEmployee that inherits from class CommissionEmployee.  Then we present an application that creates a BasePlusCommissionEmployee object and demonstrates that it has all the capabilities of the base class and the derived class, but calculates its earnings differently. © by Pearson Education, Inc. All Rights Reserved.

 Consider class CommissionEmployee (Fig. 10.4).  The Public services of class CommissionEmployee include: ◦ a constructor to initialize values ◦ properties FirstName and LastName SocialSecurityNumber, GrossSales, and CommissionRate ◦ methods CalculateEarnings and ToString  The class also declares Private instance variables grossSalesValue and commissionRate-Value © by Pearson Education, Inc. All Rights Reserved.

Defining Base Class

© by Pearson Education, Inc. All Rights Reserved.

 Method CalculateEarnings and Declaring Methods Overridable ◦ Method CalculateEarnings (lines 55–57) calculates a CommissionEmployee ’s earn-ings. ◦ A base-class method must be declared Overridable if a derived class should be allowed to override the method with a version more appropriate for that class. © by Pearson Education, Inc. All Rights Reserved.

 When we create class BasePlusCommissionEmployee, we’ll want to override (redefine) CommissionEmployee ’s Calculate-Earnings method to customize the earnings calculation for a BasePlusCommissionEmployee.  For this reason, we declared CalculateEarnings as Overridable in line 55.  In BasePlusCommissionEmployee, we’ll declare method CalculateEarnings with the keyword Overrides. © by Pearson Education, Inc. All Rights Reserved.

 Method ToString and Overriding Base Class Methods: ◦ Method ToString (lines 60–66) returns a String containing information about the CommissionEmployee. ◦ The keyword Overrides (line 60) indicates that this method overrides (redefines) the version of ToString that was inherited from CommissionEmployee ’s base class (that is, Object ). © by Pearson Education, Inc. All Rights Reserved.

 Most of a BasePlusCommissionEmployee ’s capabilities are similar, if not identical, to the those of class CommissionEmployee  Both classes require instance variables for the first name, last name, social security number, gross sales and commission rate, and properties and methods to manipulate that data.  To create class BasePlusCommissionEmployee without using inheritance, we probably would have copied the code from class CommissionEmployee and pasted it into class BasePlusCommissionEmployee, then modified the new class to include a base salary instance variable, and the methods and properties that manipulate the base salary, including a new CalculateEarnings method. © by Pearson Education, Inc. All Rights Reserved.

 Declaring Class BasePlusCommissionEmployee (Fig. 10.6) ◦ A BasePlusCommissionEmployee is a CommissionEmployee (because inheritance passes on the capabilities of class CommissionEmployee ), but class it also has  An instance variable baseSalaryValue  A property BaseSalary.  Also, BasePlusCommissionEmployee provides ◦ a new constructor ◦ a customized version of method CalculateEarnings ◦ a customized version of method ToString © by Pearson Education, Inc. All Rights Reserved.

Inherits properties and methods of CommissionEmployee

© by Pearson Education, Inc. All Rights Reserved.

 Inheriting from Class CommissionEmployee  Keyword Inherits of the class declaration indicates that class BasePlusCommissionEmployee inherits all of the Public members as well as Protected members of class CommissionEmployee.  We do not redeclare the base class’s Private instance variables—these are nevertheless present (but hidden) in derived class objects. © by Pearson Education, Inc. All Rights Reserved.

 BasePlusCommissionEmployee Constructor ◦ Each derived-class constructor must implicitly or explicitly call its base-class constructor to ensure that the instance variables inherited from the base class are properly initialized. ◦ BasePlusCommissionEmployee ’s six-argument constructor explicitly calls class CommissionEmployee ’s five-argument constructor to initialize the base class portion of a BasePlusCommissionEmployee object (that is, the five instance variables from class CommissionEmployee ). © by Pearson Education, Inc. All Rights Reserved.

 Overriding Method CalculateEarnings ◦ Class BasePlusCommissionEmployee ’s CalculateEarnings method overrides class CommissionEmployee ’s CalculateEarnings method to calculate the earnings of a base-salaried commission employee. ◦ The new version obtains the portion of the employee’s earnings based on commission alone by calling CommissionEmployee ’s CalculateEarnings method with the expression MyBase.CalculateEarnings(). © by Pearson Education, Inc. All Rights Reserved.

 Figure 10.6 below tests derved class BasePlusCommissionEmployee.  Lines 9–10 create a BasePlusCommissionEmployee object and pass "Bob", "Lewis", " ", 5000, 0.04 and 300 to the constructor as the first name, last name, social security number, gross sales, commission rate and base salary, respectively. © by Pearson Education, Inc. All Rights Reserved.

Using properties of derived object: employee

© by Pearson Education, Inc. All Rights Reserved. Using Calculatearning and ToString methods of derived object: employee

 Lines 13–22 use BasePlusCommissionEmployee ’s properties to output the object’s data.  Notice that we’re able to access all of the Public properties of classes CommissionEmployee and BasePlusCommissionEmployee here.  Lines 25–26 calculate and display the BasePlusCommissionEmployee ’s earnings by calling its Caculate-Earnings method. © by Pearson Education, Inc. All Rights Reserved.

Homework 10.9