Birds and Class Hierarchy: Object-Oriented Programming with Inheritance and Polymorphism, Slides of Java Programming

An introduction to inheritance and polymorphism in object-oriented programming (oop) using the example of a collection of birds. It covers the design of a bird class hierarchy, the concept of 'is a' relationship, and the implementation of subclasses. The document also discusses the benefits of inheritance and polymorphism, and the use of an aviary class to store and manage bird objects.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

kolii
kolii 🇮🇳

21 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Inheritance and OOP
Chapter 11
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Birds and Class Hierarchy: Object-Oriented Programming with Inheritance and Polymorphism and more Slides Java Programming in PDF only on Docsity!

Inheritance and OOP

Chapter 11

Chapter Contents

Chapter Objectives

11.1 Introductory Example: A Trip to the

Aviary

11.2 Inheritance and Polymorphism

11.3 Example: Geological Classification

11.4 Example: An O-O Payroll Program

11.5 Graphical/Internet Java: A Function

Plotter

Objects

 Organized into groups with similar

characteristics

 they are said to be related by a common characteristic

 Object-Oriented programming seeks to

provide mechanisms for modeling these

relationships

 this is where the Java word extends is used

11.1 Intro Example: A Trip to the Aviary

 Consider a collection of birds which have

different properties

 name  color (some of the same name are of different colors)  they eat different things  they make different noises  some make multiple kinds of sounds

We seek a program to simulate this collection

Hierarchy Bird

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

WalkingBird call:? color:? food:? movement:walked

FlyingBird call:? color:? food:? movement:flew

Goose call: honk color: gray food: bugs

Ostrich call: neek-neek color: brown food: grass

Parrot call: Squawk color:? food: fruit

Owl call:? color:? food:mice

TalkingParrot

...

Coding

 Note Bird class, Figure 11.

 Attributes common to all birds

 color  food  movement

Subclasses

// FlyingBird provides subclass of Bird

abstract class FlyingBird extends Bird { public FlyingBird (String color, String food) { super (color, food, "flying"); } }

Values passed to Bird class constructor where used to initialize attribute variables defined in class Bird

Subclasses

// Parrot provides subclass of FlyingBird class Parrot extends FlyingBird { public Parrot(String color) { super(color, "fruit"); } public String getCall() { return "Squawk!"; } }

Note "is a" relationship: a parrot is a flying bird. Similarly, a Parrot is a Bird

Movement attribute not an argument for constructor, a parrot is already specified as a flying bird

11.2 Inheritance and Polymorphism

 Declaring subclasses

class B extends A {... }  means class B is a specialization of class A  the "is a" relationship exists  a B object is an A object

A

B

"is a"

increasingly general

increasingly specialized

Superclass

Subclass

Inheritance

 Other names:

 superclass also called "parent class"  subclass also called "child class"

 These names help understand concept of

inheritance

 Child class inherits characteristics of

parent class

 attributes  methods docsity.com

Results of Inheritance

 Used to eliminate redundant coding

 When we send toString() message to

a Goose or Parrot or TalkingParrot

object

 none of these classes implement the

toString() method

 but … they inherit it from Bird

 toString() need not be redefined in the

subclasses docsity.com

Handles and extends

 Consider the declaration:

Bird abird = new Goose();

 this is legal  a Goose object "is a" Bird object

 Contrast

Goose aGoose = new Bird("gray",

"walking", "bugs");

 this is NOT legal  A Bird object is not necessarily a Goose object

extends is unidirectional. A extends B does NOT imply that B extends A

Polymorphism

 A method defined in a class is inherited by all descendants of that class

 When a message is sent to an object to use method m() , any messages that m() sends will also be sent to the same object

 If the object receiving a message does not have a definition of the method requested, an inherited definition is invoked

 If the object receiving a message has a definition of the requested method, that definition is invoked

Principles:

Java Hierarchy

 The classes we have been using

throughout the whole text are organized

into a hierarchy

 Object is the common ancestor

 every class inherits characteristics of this class  for example: clone() , equals() , getClass() and toString()

 Every class must fit within the Java class

hierarchy