Dale Roberts Object Oriented Programming using Java Object Oriented Programming using Java - Inheritance Overview - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected]Department of Computer and Information Science, School of Science, IUPUI
24
Embed
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected].
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Dale Roberts
Object Oriented Programming using JavaObject Oriented Programming using Java
Create new class from existing classCreate new class from existing classAbsorb existing class’s data and behaviorsAbsorb existing class’s data and behaviors
Enhance with new capabilitiesEnhance with new capabilities
More specialized group of objectsMore specialized group of objects
Behaviors inherited from superclassBehaviors inherited from superclass
Can customizeCan customize
Additional behaviorsAdditional behaviors
Dale Roberts3
Introduction (Cont.)Introduction (Cont.)
Class hierarchyClass hierarchyDirect superclassDirect superclass
Inherited explicitly (one level up hierarchy)Inherited explicitly (one level up hierarchy)
Indirect superclassIndirect superclassInherited two or more levels up hierarchyInherited two or more levels up hierarchy
Single inheritanceSingle inheritanceInherits from one superclassInherits from one superclass
Multiple inheritanceMultiple inheritanceInherits from multiple superclassesInherits from multiple superclasses
Java does not support multiple inheritanceJava does not support multiple inheritance
Dale Roberts4
Superclasses and subclassesSuperclasses and subclassesSuperclasses and subclassesSuperclasses and subclasses
Object of one class “is an” object of another classObject of one class “is an” object of another classExample: Rectangle is quadrilateral.Example: Rectangle is quadrilateral.
Class Class RectangleRectangle inherits from class inherits from class QuadrilateralQuadrilateralQuadrilateralQuadrilateral: superclass: superclassRectangleRectangle: subclass: subclass
Superclass typically represents larger set of objects than subclassesSuperclass typically represents larger set of objects than subclassesExample: Example:
First name, last name, SSN, commission rate, gross sale First name, last name, SSN, commission rate, gross sale amountamount
Base salaryBase salary
Dale Roberts11
Creating and Using a Creating and Using a CommissionEmployeeCommissionEmployee Class Class
Class Class CommissionEmployeeCommissionEmployeeExtends class Extends class ObjectObject
Keyword Keyword extendsextends
Every class in Java extends an existing classEvery class in Java extends an existing class
Except Except ObjectObject
Every class inherits Every class inherits ObjectObject’s methods’s methods
New class implicitly extends ObjectNew class implicitly extends Object
If it does not extend another classIf it does not extend another class
Dale Roberts12
Common Programming Error 9.1Common Programming Error 9.1
It is a syntax error to override a method It is a syntax error to override a method with a more restricted access modifierwith a more restricted access modifier——aa public public method of the superclass cannot method of the superclass cannot become abecome a protectedprotected oror privateprivate method in method in the subclass; a the subclass; a protectedprotected method of the method of the superclass cannot become a superclass cannot become a privateprivate method in the subclass. Doing so would method in the subclass. Doing so would break the “is-a” relationship in which it is break the “is-a” relationship in which it is required that all subclass objects be able required that all subclass objects be able to respond to method calls that are made to respond to method calls that are made toto publicpublic methods declared in the methods declared in the superclass.(cont…)superclass.(cont…)
Dale Roberts13
Common Programming Error 9.1Common Programming Error 9.1
If aIf a publicpublic method could be overridden as method could be overridden as aa protectedprotected oror privateprivate method, the method, the subclass objects would not be able to subclass objects would not be able to respond to the same method calls as respond to the same method calls as superclass objects. Once a method is superclass objects. Once a method is declareddeclared publicpublic in a superclass, the in a superclass, the method remainsmethod remains publicpublic for all that class’s for all that class’s direct and indirect subclasses. direct and indirect subclasses.
Use Use protectedprotected instance variables instance variablesEnable class Enable class BasePlusCommissionEmployeeBasePlusCommissionEmployee to to directly access superclass instance variablesdirectly access superclass instance variables
Superclass’s Superclass’s protectedprotected members are inherited by all members are inherited by all subclases of that superclasssubclases of that superclass
Dale Roberts
15OutlineOutline
CommissionCommission
Employee3.jEmployee3.javaava
(1 of 4)(1 of 4)
Lines 6-10Lines 6-10
1 // Fig. 9.12: CommissionEmployee3.java
2 // CommissionEmployee3 class represents a commission employee.
Use get methods to obtain the values of instance variables
Invoke an overridden superclass method from a subclass
Invoke an overridden superclass method from a subclass
Dale Roberts21
Common Programming Error 9.3 Common Programming Error 9.3
When a superclass method is overridden When a superclass method is overridden in a subclass, the subclass version often in a subclass, the subclass version often calls the superclass version to do a calls the superclass version to do a portion of the work. Failure to prefix the portion of the work. Failure to prefix the superclass method name with the keyword superclass method name with the keyword supersuper and a dot ( and a dot (..) separator when ) separator when referencing the superclass’s method referencing the superclass’s method causes the subclass method to call itself, causes the subclass method to call itself, creating an error called infinite recursion. creating an error called infinite recursion. Recursion, used correctly, is a powerful Recursion, used correctly, is a powerful capability discussed in Chapter 15, capability discussed in Chapter 15, Recursion.Recursion.
Dale Roberts
22OutlineOutline
BasePlusCoBasePlusCommissionEmmmissionEmployeeTest4.jployeeTest4.javaava(1 of 2)(1 of 2)
29 employee.setBaseSalary( 1000 ); // set base salary
30
31 System.out.printf( "\n%s:\n\n%s\n",
32 "Updated employee information obtained by toString",
33 employee.toString() );
34 } // end main
35 } // end class BasePlusCommissionEmployeeTest4 Employee information obtained by get methods: First name is Bob Last name is Lewis Social security number is 333-33-3333 Gross sales is 5000.00 Commission rate is 0.04 Base salary is 300.00 Updated employee information obtained by toString: base-salaried commission employee: Bob Lewis social security number: 333-33-3333 gross sales: 5000.00 commission rate: 0.04 base salary: 1000.00
Use BasePlusCommissionEmployee4 set method to modify private instance variable baseSalary.
Dale Roberts
AcknowledgementsAcknowledgementsDeitel, Java How to Program