Item Oriented Programming: Inheritance .


53 views
Uploaded on:
Category: Education / Career
Description
2. . Say not you know another completely, till you have partitioned a legacy with him.Johann Kasper LavaterThis system is to characterize as the quantity of a class the class of all classes like the given class.Bertrand RussellGood as it is to acquire a library, it is ideal to gather one. Augustine BirrellSave base power from others\' books.William Shakespeare.
Transcripts
Slide 1

9 Object-Oriented Programming: Inheritance

Slide 2

Say not you know another altogether, till you have isolated a legacy with him. Johann Kasper Lavater This strategy is to characterize as the quantity of a class the class of all classes like the given class. Bertrand Russell Good as it is to acquire a library, it is ideal to gather one. Augustine Birrell Save base specialist from others\' books. William Shakespeare

Slide 3

OBJECTIVES In this section you will realize: How legacy advances programming reusability. The thoughts of superclasses and subclasses. To utilize watchword reaches out to make a class that acquires characteristics and practices from another class. To utilize get to modifier secured to give subclass techniques access to superclass individuals. To get to superclass individuals with super . How constructors are utilized as a part of legacy chains of importance. The techniques for class Object , the immediate or backhanded superclass of all classes in Java.

Slide 4

9.1 Introduction 9.2 Superclasses and Subclasses 9.3 secured Members 9.4 Relationship amongst Superclasses and Subclasses 9.4.1 Creating and Using a CommissionEmployee Class 9.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance 9.4.3 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy 9.4.4 CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy Using ensured Instance Variables 9.4.5 CommissionEmployee – B asePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables

Slide 5

9.5 Constructors in Subclasses 9.6 Software Engineering with Inheritance 9.7 Object Class 9.8 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels 9.9 Wrap-Up

Slide 6

9.1 Introduction Inheritance Software reusability Create new class from existing class Absorb existing class\' information and practices Enhance with new capacities Subclass amplifies superclass Subclass More particular gathering of items Behaviors acquired from superclass Can modify Additional practices

Slide 7

9.1 Introduction (Cont.) Class progression Direct superclass Inherited unequivocally (one level up chain of importance) Indirect superclass Inherited at least two step up pecking order Single legacy Inherits from one superclass Multiple legacy Inherits from numerous superclasses Java does not bolster different legacy

Slide 8

9.2 Superclasses and subclasses Superclasses and subclasses Object of one class "is a" protest of another class Example: Rectangle is quadrilateral. Class Rectangle acquires from class Quadrilateral : superclass Rectangle : subclass Superclass normally speaks to bigger arrangement of items than subclasses Example: superclass: Vehicle Cars, trucks, vessels, bikes, … subclass: Car Smaller, more-particular subset of vehicles

Slide 9

Fig. 9.1 | Inheritance illustrations.

Slide 10

9.2 Superclasses and subclasses (Cont.) Inheritance chain of command Inheritance connections: tree-like progressive system structure Each class gets to be superclass Supply individuals to different classes OR subclass Inherit individuals from different classes

Slide 11

Fig. 9.2 | Inheritance chain of command for college CommunityMember s

Slide 12

Fig. 9.3 | Inheritance chain of command for Shape s .

Slide 13

9.3 secured Members ensured get to Intermediate level of insurance amongst open and private secured individuals available by superclass individuals subclass individuals Class individuals in a similar bundle Subclass access to superclass part Keyword super and a speck (.)

Slide 14

Software Engineering Observation 9.1 Methods of a subclass can\'t specifically get to private individuals from their superclass. A subclass can change the condition of private superclass occurrence factors just through non - private strategies gave in the superclass and acquired by the subclass.

Slide 15

Software Engineering Observation 9.2 Declaring private case factors helps developers test, investigate and accurately alter frameworks. In the event that a subclass could get to its superclass\' private occurrence factors, classes that acquire from that subclass could get to the occasion factors too. This would engender access to what ought to be private occurrence factors, and the advantages of data covering up would be lost.

Slide 16

9.4 Relationship amongst Superclasses and Subclasses Superclass and subclass relationship Example: CommissionEmployee/BasePlusCommissionEmployee legacy chain of importance CommissionEmployee First name, last name, SSN, commission rate, net deal sum BasePlusCommissionEmployee First name, last name, SSN, commission rate, net deal sum Base pay

Slide 17

9.4.1 Creating and Using a CommissionEmployee Class CommissionEmployee Extends class Object Keyword broadens Every class in Java expands a current class Except Object Every class acquires Object \'s techniques New class verifiably augments Object If it doesn\'t amplify another class

Slide 18

Software Engineering Observation 9.3 The Java compiler sets the superclass of a class to Object when the class statement does not expressly augment a superclass.

Slide 19

Class CommissionEmployee broadens class Object Invoke strategies setGrossSales and setCommissionRate to approve information Declare private example factors Outline CommissionEmployee.java (1 of 4) Line 4 Lines 6-10 Line 16 Lines 17-21 Lines 20-21 Implicit call to Object constructor Initialize case factors

Slide 20

Outline CommissionEmployee.java (2 of 4)

Slide 21

Calculate profit Outline CommissionEmployee.java (3 of 4) Lines 85-88

Slide 22

Override technique toString of class Object Outline CommissionEmployee.java (4 of 4) Lines 91-98

Slide 23

Common Programming Error 9.1 It is a linguistic structure blunder to supersede a strategy with a more limited get to modifier — an open technique for the superclass can\'t turn into a secured or private strategy in the subclass; an ensured technique for the superclass can\'t turn into a private technique in the subclass. Doing as such would break the "is-a" relationship in which it is required that all subclass items have the capacity to react to technique calls that are made to open strategies announced in the superclass.(cont… )

Slide 24

Common Programming Error 9.1 If an open strategy could be superseded as an ensured or private technique, the subclass articles would not have the capacity to react to a similar technique calls as superclass items. Once a technique is announced open in a superclass, the strategy stays open for all that class\' immediate and circuitous subclasses.

Slide 25

Instantiate CommissionEmployee protest Use CommissionEmployee \'s motivate strategies to recover the question\'s case variable qualities Use CommissionEmployee \'s set techniques to change the question\'s occasion variable qualities Outline CommissionEmployeeTest.java (1 of 2) Lines 9-10 Lines 15-25 Line 26-27

Slide 26

Implicitly call question\'s toString strategy Outline CommissionEmployeeTest.java (2 of 2) Line 30 Program yield

Slide 27

9.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance Class BasePlusCommissionEmployee Implicitly augments Object Much of the code is like CommissionEmployee private occurrence factors open strategies constructor Additions private example variable baseSalary Methods setBaseSalary and getBaseSalary

Slide 28

Add case variable baseSalary Use technique setBaseSalary to approve information Outline BasePlusCommissionEmployee.java (1 of 4) Line 12 Line 24

Slide 29

Outline BasePlusCommissionEmployee.java (2 of 4)

Slide 30

Outline BasePlusCommissionEmployee.java (3 of 4)

Slide 31

Method setBaseSalary approves information and sets case variable baseSalary Method getBaseSalary gives back the estimation of case variable baseSalary Update technique profit to figure the income of a base-salaried commission representative Update technique toString to show base pay Outline BasePlusCommissionEmployee.java (4 of 4) Lines 88-91 Lines 94-97 Line 102 Lines 108-113

Slide 32

Instantiate BasePlusCommissionEmployee question Use BasePluCommissionEmployee \'s inspire techniques to recover the protest\'s case variable qualities Outline BasePlusCommissionEmployeeTest.java (1 of 2) Line 9-11 Lines 16-27

Slide 33

Use BasePlusCommissionEmployee \'s setBaseSalary techniques to set base compensation Explicitly call question\'s toString technique Outline BasePlusCommissionEmployeeTest.java (2 of 2) Line 29 Line 33 Program yield

Slide 34

Software Engineering Observation 9.4 Copying and sticking code starting with one class then onto the next can spread mistakes over various source code records. To abstain from copying code (and perhaps mistakes), utilize legacy, instead of the "duplicate and-glue" approach, in circumstances where you need one class to "retain" the occurrence factors and techniques for another class.

Slide 35

Software Engineering Observation 9.5 With legacy, the basic case factors and strategies for every one of the classes in the chain of command are proclaimed in a superclass. At the point when changes are required for these basic components, programming engineers require just to roll out the improvements in the superclass—subclasses then acquire the progressions. Without legacy, changes would should be made to all the source code records that contain a duplicate of the code being referred to.

Slide 36

9.4.3 Creating a CommissionEmployee-BasePlusCommiionEmployee Inheritance Hierarchy Class BasePlusCommissionEmployee2 Extends class CommissionEmployee Is a CommissionEmployee Has occurrence variable baseSalary Inherits open and ensured individuals Constructor not acquired

Slide 37

Class BasePluCommissionEmployee2 is a subclass of CommissionEmployee Invoke the superclass constructor utilizing the superclass constructor call language structure Outline BasePlusCommissionEmployee2.java (1 of 3) Line 4 Li

Recommended
View more...