220 likes | 390 Views
Object Oriented Programming: Inheritance. Chapter 9. What You Will Learn. Software reusability (Recycling) Inheriting data members and methods from previously defined classes. Introduction. Software Reusability saves time in program development encourages use of proven, debugged code
E N D
Object Oriented Programming: Inheritance Chapter 9
What You Will Learn • Software reusability(Recycling) • Inheriting data members and methods from previously defined classes
Introduction • Software Reusability • saves time in program development • encourages use of proven, debugged code • reduces problems • Write programs in general fashion • Enables software designers to deal with complexity of modern software
Introduction • When creating a new class … • designate that class to inherit data members, functions of previously defined superclass • result is a subclass • Subclass adds new data members and functions • Replace and refine existing members
Base Classes & Derived Classes • Superclass is more general • student, shape, loan • Subclass is more specific • grad student, undergrad • circle, triangle, rectangle • carloan, home improvement, mortgage • Some languages talk of • Base class (Superclass) • Derived class (Subclass)
Superclass and Subclass • Inheritance produces tree like structures
Superclass and Subclass • Inheritance produces tree like structures
Design Tip • Important link between subclass and superclass • The “IS-A” relationship • Examples • A checking account IS-A banking account • A savings account IS NOT a checking account • If there is no IS-A relationship, do not use inheritance Sun comments on the concept of inheritance
protected Members • protected access • Intermediate level of protection between public and private • protected members accessible by • superclass members • subclass members • Class members in the same package • Subclass access to superclass member • Keyword super and a dot (.)
Comments on Private vs. Protected • Use protected when • Superclass should provide a service only to its subclasses • Should not provide service to other clients • Use private so that • Superclass implementation can change without affecting subclass implementations • Author advocates avoiding protected • Instead provide set and get methods to access private data items (see Figures 9.12, 9.13 in text)
Relationship between Superclasses and Subclasses • Superclass and subclass relationship • Example: CommissionEmployee/BasePlusCommissionEmployee inheritance hierarchy • CommissionEmployee • First name, last name, SSN, commission rate, gross sale amount • BasePlusCommissionEmployee • First name, last name, SSN, commission rate, gross sale amount • Base salary
Creating and Using a CommissionEmployee Class • Class CommissionEmployee • Extends class Object • Keyword extends • Every class in Java extends an existing class • Except Object • Every class inherits Object’s methods • New class implicitly extends Object • If it does not extend another class
Creating and Using a CommissionEmployee Class • Class CommissionEmployee • Extends class Object • Keyword extends • Every class in Java extends an existing class • Except Object • Every class inherits Object’s methods • New class implicitly extends Object • If it does not extend another class • View class Figure 9.4 • View test program, Figure 9.5
Creating a BasePlusCommissionEmployee Class without Using Inheritance • Class BasePlusCommissionEmployee • Implicitly extends Object • Much of the code is similar to CommissionEmployee • private instance variables • public methods • constructor • Additions • private instance variable baseSalary • Methods setBaseSalary and getBaseSalary View classFigure 9.6 View test programFigure 9.7
Creating a CommissionEmployee-BasePlusCommiionEmployee Inheritance Hierarchy • Class BasePlusCommissionEmployee2 • Extends class CommissionEmployee • Is a CommissionEmployee • Has instance variable baseSalary • Inherits public and protected members • Constructor not inherited • Note these points plus invalid references, Figure 9.8 – note compiler error messages
Using protected Instance Variables • Use protected instance variables • Enable class BasePlusCommissionEmployee to directly access superclass instance variables • Superclass’s protected members are inherited by all subclases of that superclass • View version that now works, Figure 9.10 • Test program, Figure 9.11
Using protected Instance VariablesAdvantages • Subclassescan modify values directly • Slight increase in performance • Avoid set/get method call overhead
Using protected Instance VariablesDisadvantages • No validity checking • subclass can assign illegal value • Implementation dependent • subclass methods more likely dependent on superclass implementation • superclass implementation changes may result in subclass modifications • Fragile (brittle) software
Reexamine Hierarchy • Use the best software engineering practice • Declare instance variables as private • Provide public get and set methods • Use get method to obtain values of instance variables • View new version of CommissionEmployee, Figure 9.12 • New version of BasePlusCommissiionEmployee, Figure 9.13 • Test program, Figure 9.14
Instantiating Subclass Object Chain Of Constructor Calls • Subclass constructor invokes superclass constructor • Implicitly or explicitly • Base of inheritance hierarchy • Last constructor called in chain is Object’s constructor • Original subclass constructor’s body finishes executing last
Instantiating Subclass Object Chain Of Constructor Calls • Example, Figure 9.15 : CommissionEmployee3-BasePlusCommissionEmployee4 hierarchy • CommissionEmployee3 constructor called second last (last is Object constructor) • CommissionEmployee3 constructor’s body finishes execution second (first is Object constructor’s body) • Figure 9.16, BasePlusCommissionEmploye5 • Figure 9.17, Test program, demonstrates constructor calls
Software Engineering with Inheritance • At the design stage, certain classes found to be closely related • Factor out common attributes, behaviors • Place these in a superclass • Use inheritance to develop subclasses with inherited capabilities • This avoids proliferation of classes, "reinventing the wheel" • Note • Declaring a subclass does not affect superclass source code