1 what is inheritance? a form of software reuse create new class from existing class absorb existing...

18
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class’ data and behaviors Enhance with new or modified capabilities Used to eliminate redundant code Example Circle class inherits from Shape class Circle extends Shape

Upload: angelina-caldwell

Post on 27-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

1

What is Inheritance?

A form of software reuse

Create new class from existing classAbsorb existing class’ data and behaviors

Enhance with new or modified capabilities

Used to eliminate redundant code

ExampleCircle class inherits from Shape class

Circle extends Shape

Page 2: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

2

Subclass and Superclass

Subclass extends superclassSubclass

– Also called child class or derived class

– More specialized group of objects

– Inherits data and behaviors from superclass

– Can add or modify behaviors (methods)

Superclass – Also called parent class or base class

– Typically represents larger group of objects

– Supplies data and behaviors to subclass

– Direct and indirect

Single and multiple inheritance

Page 3: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

3

The Object class

Top of the Java class hierarchy

Located in package java.lang

Class from which every other Java class inherits

A class implicitly extends Object if no other class is specified

.toString(), .clone(), .equals()equals() compares addresses, not the contents of objects

Page 4: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

4

“is-a” vs. “has-a” relationships“is-a”

Represents inheritance

subclass object is an example of the superclass object

Example: a Car is a Vehicle

Car is subclass; Vehicle is superclass

Keywords: extends, implements

“has-a”Represents composition

Object contains one or more objects of other classes as members

Example: Car has a Steering Wheel

Page 5: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

5

Composition

Dog “has-a” owner, mother, father, leash…

public class Dog{ private String name; private int age; private Person owner; private Dog mother, father; private Leash dogLeash;}

Page 6: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

6

Inheritance

Animal’s Stuff

Animal’s Stuff

Dog’s Stuff

Animal

Dog

Dog extends (“is-a”) Animal

Page 7: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

7

public class Animal{ private int numLegs;

public int getLegs() { return numLegs; }

public void setLegs(int legs) { numLegs = legs; }

public String noise() { return “?”; }}

public class Dog extends Animal{ public static void main (String[] args) { Dog d = new Dog(); d.setLegs(4); System.out.println ("A dog has "

+ d.getLegs() + " legs"); System.out.println ("A dog says "

+ d.noise()); }

public String noise() { return "WOOF!"; }}

Page 8: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

8

Inheritance Examples

Superclass Subclasses Student GraduateStudent,

UndergraduateStudent Shape Circle, Triangle, Rectangle Loan CarLoan, HomeImprovementLoan,

MortgageLoan Employee Faculty, Staff BankAccount CheckingAccount,

SavingsAccount Fig. 9.1 Inheritance examples.

Page 9: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

9

Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

CommunityMember

Employee Student

StaffFaculty

Administrator Teacher

Alumnus

An Inheritance (Class) Hierarchy

Page 10: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

10

Draw an Inheritance Hierarchy for these classes: Triangle

Sphere TwoDimensionalShape ShapePyramidSquareThreeDimensionalShapeCubeCircleSphere

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Pyramid

Page 11: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

11

Try one more! WalkingBirdOwlOstrichParrotBirdFlyingBirdChickenTalkingParrot

Bird

WalkingBird FlyingBird

Ostrich Chicken Parrot Owl

TalkingParrot

Page 12: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

12

Strategy

Design classes for objects

Identify characteristics classes have in common

Abstraction: focus on commonalities among objects in a system

Design superclasses to store common characteristics

Page 13: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

Bird

call: ?color:?food:?movement:?

WalkingBird

call: ?color:?food:?movement:walk

FlyingBird

call: ?color:?food:?movement:fly

Chicken

call: cluckcolor: whitefood: bugs

Ostrich

call: neek-neekcolor: brownfood: grass

Parrot

call: Squawkcolor:?food: fruit

Owl

call: hoocolor: brownfood:mice

TalkingParrot. . .

Page 14: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

14

protected Members

Variables or methods available only to derived classesIntermediate level of protection between public and privateAccessible to

superclass memberssubclass membersclass members in the same package

Use super. to access a superclass method that has been overridden by a subclass method.Don’t use protected instance variables!

“Fragile” software can “break” if superclass changes

Page 15: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

15

protected Members

ClassA

public m1()private m2()protected m3()

ClassB

ClassA.m1()

ClassC

ClassA.m1()ClassA.m3()

is-a has-a

Page 16: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

16

Constructors in Subclasses

Constructors are not inherited!

Chain of constructor callssubclass constructor invokes superclass constructor

– Implicitly or explicitly– To call explicitly, use super()– Superclass constructor call must be first

statement in subclass constructor

Object constructor is always fired last

Example: Student extends Person

Page 17: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

17

Software Engineering with Inheritance

Customizing existing softwareInherit from existing classes

Include additional members

Redefine superclass members

No direct access to superclass source code

Independent software vendors (ISVs)Develop proprietary code for sale/license

Users derive new classes – Without accessing ISV proprietary source code

Page 18: 1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified

18

Quiz

_________ methods are not inherited.

The _____ class is at the top of the Java hierarchy.

The _____ keyword is used in Java to represent inheritance.

A Java class _____ inherit from more than one class.

A variable or method that is available only to derived classes is called a____________.

Private/Static/Constructor

Object

extends

cannot

protected member