csci 143 oop – inheritance 1. what is inheritance? a form of software reuse create a new class...

18
CSCI 143 OOP – Inheritance 1

Post on 20-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

CSCI 143

OOP – Inheritance

1

Page 2: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

What is Inheritance?

• A form of software reuse• Create a new class from an existing class

– Absorb existing class data and methods– Enhance with new or modified capabilities

• Used to eliminate redundant code• Example

– Dog class inherits from Animal class– Dog extends Animal

2

Page 3: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Subclass and Superclass– Subclass extends superclass

• Subclass – Also called child class or derived class– More specialized group of objects– Inherits data and methods from superclass– Can add or modify methods

» Modifying methods is called overriding• Superclass

– Also called parent class or base class– Typically represents larger group of objects– Supplies data and behaviors to subclass– May be direct or indirect

– Java does not support multiple inheritance3

Page 4: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

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()

4

Page 5: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

“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

5

Page 6: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Composition

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

6

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

Page 7: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Inheritance• Dog extends (“is-a”) Animal

7

Animal’s Stuff

Animal’s Stuff

Dog’s Stuff

Animal

Dog

Page 8: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

8

public class Animal{ private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; }

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

public class Dog extends Animal{ public String talk() { return "WOOF!"; }

public void fetch(String toy) { System.out.println(“Fetching a ” + toy); }}

This is an overridden method, Inherited from animal.This is an overridden method, Inherited from animal.

This is a new method.This is a new method.

Page 9: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

public class PetStore { public static void main (String[] args) {

Dog d = new Dog(); d.setName(“Henry”);

System.out.println (d.getName() + " says " + d.talk());

}}

9

Henry says WOOF!

Page 10: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

An Inheritance (Class) Hierarchy

10

Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

CommunityMember

Employee Student

StaffFaculty

Administrator Teacher

Alumnus

Page 11: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Draw an Inheritance Hierarchy for these

classes:

11

TriangleSphere 2DShape ShapePyramidSquare3DShapeCubeCircle

Page 12: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Create an inheritance hierarchy…

• Automobile• Plant• Bird• ???

12

Page 13: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

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

13

Page 14: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

14

Inherited Not InheritedPublic members ConstructorsPrivate data Private methodsProtected members Static members

Page 15: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

protected Members– Intermediate level of protection between public and private

– Accessible to• superclasses• subclasses• classes 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

15

Page 16: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

protected Members

16

Dog

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

Owner

dog.m1()

Poodle

dog.m1()dog.m3()

is-a has-a

Page 17: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

Constructors in Subclasses

– Constructors are not inherited!– Chain of constructor calls

• subclass 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

– All instance variables are inherited• Private variables not directly accessible

17

Page 18: CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and

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 within an

inheritance hierarchy and its package is a____________.

18

Private/Static/Constructor

Object

extends

cannot

protected member