Week 5 Lecture 05
Week 5 Lecture 05
Week 5 Lecture 05
Object-Oriented Programming
Lecture 5
I further acknowledge the Traditional Owners of the country on which you are on
and pay respects to their Elders, past, present and future.
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of the
University of Sydney pursuant to Part VB of the Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright under the Act. Any
further copying or communication of this material by you may be the subject of
copyright protection under the Act.
Do not remove this notice.
● Inheritance basics
Subclass/
● Encapsulation Cat Dog
Child
● Programming Inheritance
● Type information
Syntax:
[public] class ClassName extends SuperClassName
Refer to Chapter 8.1, page 624-629 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Part of our class declaration line allows for us to define what class
we want to extend from
Refer to Chapter 8.1, page 624-629 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
We have used the public and private access modifier but we will now
use the protected access modifier.
Like private it will not be accessible to other classes but now with the
exception inherited classes.
Refer to Chapter 8.1, page 632 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
}
public boolean isBroken() {
return shattered;
}
}
}
public boolean isBroken() {
Subclass will have access return shattered;
to any protected and }
public methods.
}
}
public boolean isBroken() {
return shattered;
}
}
}
public boolean isBroken() {
return shattered;
}
}
}
public boolean isBroken() {
return shattered;
}
}
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
}
However! Nothing was initialised, so all we
get are default values
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
public class Bottle {
protected String name;
public class GlassBottle extends Bottle {
protected double width;
protected double height;
protected double depth;
protected double litresFilled;
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
public class Bottle {
protected String name;
public class GlassBottle extends Bottle {
protected double width;
protected double height;
protected double depth;
protected double litresFilled;
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
public class Bottle {
protected String name;
public class GlassBottle extends Bottle {
protected double width;
protected double height;
public GlassBottle() {
protected double depth;
this.name = "Glass Bottle";
protected double litresFilled;
}
public Bottle() {
private boolean shattered = false;
this.name = "Basic Bottle";
this.width = 10.0;
public void shatter() {
this.height = 10.0;
shattered = true;
this.depth = 10.0;
}
this.litresFilled = 0;
}
public boolean isBroken() {
return shattered;
}
public double volume() {
}
return height*width*depth;
}
What if we were to define a constructor in
} the subclass?
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
public class Bottle {
protected String name;
public class GlassBottle extends Bottle {
protected double width;
protected double height;
public GlassBottle() {
protected double depth;
this.name = "Glass Bottle";
protected double litresFilled;
}
public Bottle() {
private boolean shattered = false;
this.name = "Basic Bottle";
this.width = 10.0;
public void shatter() {
this.height = 10.0;
shattered = true;
this.depth = 10d;
By default, when a subclass object is created, it will} refer to the super class’s constructor.
this.litresFilled = 0;
}
public boolean isBroken() {
> java MyProgram
return shattered;
public static void main(String[] args) {
GlassBottle b = new GlassBottle();
public double volume() { 1000.0}
System.out.println(b.volume()); }
return height*width*depth; Glass Bottle
System.out.println(b.name);
}
}
} Providing some values we can inspect the <program end>
previous code segment
We can see that we called the GlassBottle constructor
The University of Sydney and it set the name to Glass Bottle. Page 22 22
Inheritance
Assuming the default constructor is given to the superclass, the subclass does
not need to define one.
public class Bottle {
protected String name;
public class GlassBottle extends Bottle {
protected double width;
protected double height;
public GlassBottle() {
protected double depth;
this.name = "Glass Bottle";
protected double litresFilled;
}
public Bottle() {
private boolean shattered = false;
this.name = "Basic Bottle";
this.width = 10.0;
public void shatter() {
this.height = 10.0;
shattered = true;
this.depth = 10d;
By default, when a subclass object is created, it will} refer to the super class’s constructor.
this.litresFilled = 0;
}
public boolean isBroken() {
> java MyProgram
return shattered;
public static void main(String[] args) {
GlassBottle b = new GlassBottle();
public double volume() { 1000.0}
System.out.println(b.volume()); }
return height*width*depth; Glass Bottle
System.out.println(b.name);
}
}
} Providing some values we can inspect the <program end>
previous code segment
Hang on! If we called GlassBottle() how is volume
The University of Sydney returning 1000.0? Page 23 23
Let’s try something
Joystick, Powerglove
● Super class is Media and subclasses are DVD, Book, Image
Refer to Chapter 8.1, page 635 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Refer to Chapter 8.1, page 635 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
● Method Overloading
● Constructor Overloading
● try-catch and exceptions
In regards to Java we are able to use the same method name but
with different method signature.
Simply:
For example:
Method calls:
int[] x = crossProduct((int[])null, (int[])null);
public Person() {
name = "Jeff";
age = DEFAULT_AGE;
}
public Person() {
name = "Jeff";
age = DEFAULT_AGE;
}
public Person() {
name = "Jeff";
age = DEFAULT_AGE;
}
private static int DEFAULT_AGE = 21; private static int DEFAULT_AGE = 21;
private String name; private String name;
private int age; private int age;
private static int DEFAULT_AGE = 21; private static int DEFAULT_AGE = 21;
private String name; private String name;
private int age; private int age;
private static int DEFAULT_AGE = 21; private static int DEFAULT_AGE = 21;
private String name; private String name;
private int age; private int age;
public Person() {
super(name, age);
this("Jeff", DEFAULT_AGE);
this.departmentId = departmentId;
}
this.employeeId = employeeId;
}
public Person(String name) { //<snipped other methods
this(name, DEFAULT_AGE); }
}
public Person() {
super(name, age);
this("Jeff", DEFAULT_AGE);
this.departmentId = departmentId;
}
this.employeeId = employeeId;
}
public Person(String name) { //<snipped other methods
this(name, DEFAULT_AGE); }
}
Let’s tackle the two major Exception classes that dictate how the
rest operate.