Docsity

Java Programming: Inheritance & Polymorphism with PacMan & SuperPacMan, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
11/18/2009
1
ESE
112
ESE
112
Java Programming:
Inheritance,
Inheritance,
Polymorphism
Example: PacMan and SuperPacMan
public class PacMan{
private int x;
private int y;
bliPM(it it){}
pu
bli
c
P
ac
M
an
(i
n
t
x,
i
n
t
y
){
..
}
public void eatDot(){ ..}
public void move(){.. }
public void turnLeft(){.. }
}
public class SuperPacMan extends PacMan{
ESE112 2
…
public void turnRight(){
turnLeft();
turnLeft();
turnLeft();
}
}
ESE112 2
Inheritance
One of the key concepts of OOP
A hierarchical relationship among classes
Establishes a superclass/subclass relationship
Establishes
“
is a
”
relationships
Establishes
is
a
relationships
¾e.g. a SuperPacMan “is a” PacMan
Benefits:
Reusability of code
¾Put code in one class, use it in all the subclasses
¾
Revisions only needs to be done in
place
ESE112 3
¾
Revisions
only
needs
to
be
done
in
place
Polymorphic code (works on “many forms”)
¾Write general purpose code designed for a supertype that
works for all subtypes
ESE112 3
The “extends” keyword
public class PacMan{
}
Inheritance is established via the “extends” keyword
public class SuperPacMan extends PacMan{
}
Now we say
SuperPacMan inherits from PacMan
A SuperPacMan “is a” PacMan
ESE112 4
SuperPacMan is a subclass/subtype of PacMan
PacMan is the superclass/supertype of SuperPacMan
ESE112 4
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Inheritance & Polymorphism with PacMan & SuperPacMan and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ESE112ESE

Java Programming:Inheritance,Inheritance,Polymorphism

Example: PacMan and SuperPacMan

public class PacMan{

private int x;private int y;

bli

P

M

(i t

i t

){

}

public PacMan(int x, int y){..}public void eatDot(){ ..}public void move(){.. }public void turnLeft(){.. } }

public class SuperPacMan extends PacMan{

ESE

2

… public void turnRight(){

turnLeft();turnLeft();turnLeft();

}

}

ESE

2

Inheritance

One of the key concepts of OOP

„

A hierarchical relationship among classes „

Establishes a superclass/subclass relationship „

Establishes

“is a” relationships

Establishes

is a

relationships

¾

e.g. a SuperPacMan “is a” PacMan

Benefits:

„

Reusability of code

¾

Put code in one class, use it in all the subclasses ¾

Revisions only needs to be done in 1 place

ESE

3

¾

Revisions

only needs to be done in 1 place

„

Polymorphic code (works on “many forms”)

¾

Write general purpose code designed for a supertype thatworks for all subtypes

ESE

3

The “extends” keyword

public class PacMan{}

Inheritance is established via the “extends” keyword

public class SuperPacMan extends PacMan{}

Now we say

„

SuperPacMan

inherits

from PacMan

„

A SuperPacMan “is a” PacMan

ESE

4

„

SuperPacMan is a

subclass

/ subtype

of PacMan

„

PacMan is the

superclass/supertype

of SuperPacMan

ESE

4

What can you inherit?

„

Visibility modifiers determine which classmembers are accessible and which do not

„

Members (variables and methods) declaredwith

public

visibility are accessible, and those

with

private

visibility are not

„

Problem: How to make class instance variables

ESE

5

„

Problem:

How to make class instance variables

visible only to its subclasses?^ „

Solution: Java provides a third visibility modifier thathelps in inheritance situations:

protected

ESE

5

Inheritance Tree

ESE

6

Java has

single inheritance

; each node has one parent

Except for Object which has no parent

ESE

6

The Object Class

All classes inherit from the Object class

„

The Object class is the root of the class hierarchy „

When we create a new class, “extends Object” is implied/implicit public class Car {} public class Car extends Object{}

The Object class has several methods which all object

inherit,

ESE

7

j^

j^

most notably: toString() and equals()

-^

Once we inherit these, we can also override the behavior i.e. make itconform to what the object of subclass will do when this method iscalled.

ESE

7

Need for equals(): Comparison of Strings

„

If the == operator is used for Strings

„

Java compares the addresses where the String objects arestored, not the letters in the String „

For example:

String a = "hi";String a =

hi ;

String b = "hi";result = a == bresult: false

„

Use the String class'

equals

method to compare two

Strings for equality

ESE

8

a.equals(b)

ESE

8

Method Overloading

Method

overloading

occurs when

„

A class has two or more methods with the same

name

but

different

signatures

g

¾

Different signature -> the number, order, or types of theirparameters differ

// the foo method is overloadedpublic void foo() {.. }

ESE

13

public void foo(int x) { ..}Public void foo(double x){..}public void foo(int x, double y) {..}

„

When the foo(..) method is called, Java picks the one that “matches”. E.g.

foo(10, 350.5);

13

Overriding

Overriding

occurs if

„

There are two or more methods with the samename

and the same signature

in an inheritance

h i

chain

„

For example, the Object class has a toString()method

¾

It can be

overriden

in a subclass simply by creating a

method with the same signature

public String toString() {. .}

ESE

14

„

Java picks the “lowest” method in the inheritancechain possible

14

Type Rules

„

A reference variable of type t may hold a value of its own type orany subtype (but not of a supertype). „

Given the following variable declaration:

PacMan b;PacMan b = new PacMan(0, 0);b = new String();b = new SuperPacMan(0, 0);b = new Object();

b;

Which of the following assignments are valid (compile)?How about these? ESE

15

SuperPacMan bb;bb = new PacMan(0, 0);bb = new String();bb = new SuperPacMan(0, 0);bb = new Object();

ESE

15

The “instanceof” Operator

PacMan b = new PacMan(0, 0);b instanceof PacManAnswer: trueb instanceof ObjectAnswer: trueb instanceof SuperPacManAnswer: false

ESE

16

ESE

16

The “instanceof” Operator

SuperPacMan

bb

=^

new

SuperPacMan(0,

0);

bb

instanceof SuperPacMan

Answer:

true

bb

instanceof Object

Answer:

true

bb

instanceof PacMan

Answer:

true

PacMan bbb

=^

new

SuperPacMan

(0, 0);

//a variable can store a subtype

bbb

instanceof

SuperPacMan(0, 0)

Answer:

true

bbb

instanceof

PacMan

Answer:

true

ESE

17