



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CIS
1
CIS
2
public class Bot{
private int x;private int y;public int getX() { .. }public int getY() { .. }public void eatDot(){ ..}public void move(){.. }public void turnLeft(){.. }
}
public class BetterBot extends Bot{
public void turnRight(){
turnLeft();turnLeft();turnLeft();
}
}
CIS
3
A hierarchical relationship among classes
Establishes a superclass/subclass relationship
Establishes “is a” relationships
¾
e.g. a BetterBot “is a” Bot
Reusability of code
¾
Put code in one class, use it in all the subclasses
¾
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
CIS
4
public class Bot{} public class BetterBot extends Bot{}
¾
However, based on the visibility modifiers, certain instancevariables and methods defined in Bot may not be accessible
CIS
5
CIS
6
BetterBot b = new BetterBot();
But first, a constructor from its highest ancestor (Object) is called,then the next highest (Bot), then its own (BetterBot)
The default behavior is such that the default (no-argumentconstructor) is executed
public class BetterBot extends Bot{
public BetterBot (BotWorld world){
super(world);
CIS
7
public class
Dog{
private String name;private int age;
Dog(String name, int age){
this.name = name;this.age = age;
} class
BetterDog
extends Dog{
public BetterDog(String name, int age){
super(name,age);
CIS
12
Java compares the addresses where the String objects arestored, not the letters in the String
For example:
CIS
13
public class Person {
private int social;
// social security #
private String name;
} public int getSocial() { return social; }public boolean equals(Person p) {
return this.social == p.getSocial();
CIS
14
b = new Bot();b = new String();b = new BetterBot();b = new Object();
Bot b;
BetterBot bb;bb = new Bot();bb = new String();bb = new BetterBot();bb = new Object();
CIS
15
> Bot b = new Bot();> b instanceof Bottrue> b instanceof Objecttrue> b instanceof BetterBotfalse> BetterBot bb = new BetterBot();> bb instanceof BetterBottrue> bb instanceof Objecttrue> bb instanceof Bottrue> Bot bbb = new BetterBot();
//a variable can store a subtype
> bbb instanceof BetterBot()true> bbb instanceof Bottrue
CIS
16
Inheritance Example
public class Vehicle{
protected int registrationNumber;
public int getRegNumber(){
return registrationNumber; } public void setRegNumber(int num){
registrationNumber = num; } }
public class Car extends Vehicle { //new instance variable, not inheritedprivate int numberOfDoors;public Car(int doors){
numberOfDoors = doors; } public int getDoors() {
return numberOfDoors; } }
CIS
17
Interactions I
Car c = new Car(2);> c.getDoors() 2 > c.getRegNumber() //Inherited Method 0 > c.setRegNumber(45) //Inherited Method> c.getRegNumber() 45
CIS
18
Interactions II
CIS
19
Interactions III
Vehicle v = new Car(4); //valid why?> v.setRegNumber(3)> v.getRegNumber() 3 v.getDoors()Compile Error: No getDoors() in Vehicle
Since reference type is of Vehicle and not Carthis makes sense
In other words “v” could potentially refer to othertypes of vehicles that are not cars
The solution here is to use
down type-casting
(conversion).