

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















they are said to be related by a common characteristic
this is where the Java word extends is used
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
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
...
color food movement
// 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
// 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
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
superclass also called "parent class" subclass also called "child class"
attributes methods docsity.com
none of these classes implement the
subclasses docsity.com
this is legal a Goose object "is a" Bird object
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
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:
every class inherits characteristics of this class for example: clone() , equals() , getClass() and toString()