



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 java programming using the example of pacman and superpacman classes. It covers the concept of 'is a' relationship, benefits of inheritance, the 'extends' keyword, protected visibility modifier, inheritance tree, the object class, and the need for equals() and tostring() methods. It also explains constructors and inheritance, method overloading, and method overriding.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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
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
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
ESE
5
Solution: Java provides a third visibility modifier thathelps in inheritance situations:
protected
ESE
5
ESE
6
Java has
single inheritance
; each node has one parent
Except for Object which has no parent
ESE
6
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
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
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
¾
It can be
overriden
in a subclass simply by creating a
method with the same signature
public String toString() {. .}
ESE
14
14
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
PacMan b = new PacMan(0, 0);b instanceof PacManAnswer: trueb instanceof ObjectAnswer: trueb instanceof SuperPacManAnswer: false
ESE
16
ESE
16
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