Lecture Slide
Lecture Slide
Object-Oriented Programming
Lecture 6
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.
● Abstract Classes
abstract là Animal
sum(){ System.out.println(sum());
return 1+1;
}
The main case for abstract is that we have some type that we do not want
instantiated but is a generalisation of many other types.
Example:
● Shape is a generalisation of Triangle, Square, Circle but we don’t have a
concrete instance of Shape new Shape(); XXX
● Furniture is a generalisation of Chair, Sofa, Table and Desk. new Furniture(); XXX
Refer to Chapter 8.4, pages 684-688, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
● Constructors
● Define methods (static and instance)
● Attributes
● Use all the access modifiers
● … everything a regular class can do except!
We cannot instantiate the class but we can specify methods subtypes must
define. Dog speak(); gau gau concrete class
Cat speak(); meo meo
Simply we are able to define an abstract class by using the abstract keyword.
This immediately marks the class as abstract and we do not need anything
more.
Syntax:
[modifier] abstract class ClassName
Example:
public abstract class Furniture
Since it is marked as abstract, the compiler will refuse to allow this type of
Animal Dog
instantiation.
breathe() breathe()
Concrete Class {
Abstract Class speak() // your implementation
}
eat()
The class should not be instantiated and behaviour is defined by the subtypes
and not the super type. inheritance
Within a UML class diagram, we can illustrate abstract classes with the
following.
● Interfaces
Refer to Chapter 8.4, pages 659-669, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
// abstract methods
private abstract void test();
The University of Sydney } Page 22
Declaration of an interfaces
Syntax:
[modifier] interface InterfaceName
Example:
Syntax:
[modifier] interface InterfaceName
To be clear, an interface is
Example: not a class.
Syntax:
[modifier] interface InterfaceName
Example:
Syntax:
[modifier] interface InterfaceName
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; private double kmTravelled = 0.0;
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; private double kmTravelled = 0.0;
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public classpublic
MovingAnimals {
void move(double hours) {
interface is not a class public void move(double hours) {
public static void main(String[] args) { if(region.equals("water"))
if(region.equals("water"))
Dog dog = new Dog("land");
kmTravelled += (waterSpeed_kmh *hours); We cankmTravelled
create an Move[]
+= (waterSpeed_kmh * hours);
Dolphin dolphin = new Dolphin("land");
else if(region.equals("land")) array else
andif(region.equals("land"))
add both dog {
Move[] movingAnimals = {dog, dolphin};
kmTravelled += (landSpeed_kmh * hours); and dolphin types+=to(landSpeed_kmh
kmTravelled it. * hours);
} m : movingAnimals) {
for(Move Why?}
m.move(1.0);
} public double getKMTravelled() { They both have a similar implementation but
public double getKMTravelled() {
return kmTravelled; their land and water movementreturn
speed is
kmTravelled;
System.out.println(dog.getKMTravelled());
} different. We could change it }completely
System.out.println(dolphin.getKMTravelled());
} between the two implementations.
}
}
}
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public classpublic
MovingAnimals {
void move(double hours) { public void move(double hours) {
public static void main(String[] args) { if(region.equals("water"))
if(region.equals("water"))
Dog dog = new Dog("land");
kmTravelled += (waterSpeed_kmh *hours); We cankmTravelled
create an Move[]
+= (waterSpeed_kmh * hours);
Dolphin dolphin = new Dolphin("land");
else if(region.equals("land")) array else
andif(region.equals("land"))
add both dog {
Move[] movingAnimals = {dog, dolphin};
kmTravelled += (landSpeed_kmh * hours); and dolphin types+=to(landSpeed_kmh
kmTravelled it. * hours);
} m : movingAnimals) {
for(Move Why?}
m.move(1.0); Because they are of type
} public double getKMTravelled() { Move.
They both have a similar implementation but
public double getKMTravelled() {
return kmTravelled; their land and water movementreturn
speed is
kmTravelled;
System.out.println(dog.getKMTravelled());
} different. We could change it }completely
System.out.println(dolphin.getKMTravelled());
} between the two implementations.
}
}
}
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public classpublic
MovingAnimals {
void move(double hours) { public void move(double hours) {
public static void main(String[] args) { if(region.equals("water"))
if(region.equals("water"))
Dog dog = new Dog("land"); If they of type Move we
kmTravelled += (waterSpeed_kmh *hours); kmTravelled += (waterSpeed_kmh * hours);
Dolphin dolphin = new Dolphin("land"); are guaranteed to be able
else if(region.equals("land")) else if(region.equals("land")) {
Move[] movingAnimals = {dog, dolphin}; to use move() method.
kmTravelled += (landSpeed_kmh * hours); kmTravelled += (landSpeed_kmh * hours);
} m : movingAnimals) {
for(Move }
m.move(1.0);
} public double getKMTravelled() { They both have a similar implementation but
public double getKMTravelled() {
return kmTravelled; their land and water movementreturn
speed is
kmTravelled;
System.out.println(dog.getKMTravelled());
} different. We could change it }completely
System.out.println(dolphin.getKMTravelled());
} between the two implementations.
}
}
}
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public classpublic
MovingAnimals {
void move(double hours) { public void move(double hours) {
public static void main(String[] args) { if(region.equals("water"))
if(region.equals("water"))
Dog dog = new Dog("land");
kmTravelled += (waterSpeed_kmh *hours); kmTravelled += (waterSpeed_kmh * hours);
Dolphin dolphin = new Dolphin("land");
else if(region.equals("land")) else if(region.equals("land")) {
Move[] movingAnimals = {dog, dolphin};
kmTravelled += (landSpeed_kmh * hours); kmTravelled += (landSpeed_kmh * hours);
} m : movingAnimals) {
for(Move }
m.move(1.0);
} public double getKMTravelled() { They both have a similar implementation but
public double getKMTravelled() {
We can
their land and water movement see
speed the
is updated
return kmTravelled;
return kmTravelled;
System.out.println(dog.getKMTravelled()); different. We could changevariables that have been
it }completely
}
System.out.println(dolphin.getKMTravelled()); applied to both objects.
between the two implementations.
} }
}
}
public class Dog implements Move { public class Dolphin implements Move {
private String region; //Water or Land private String region; //Water or Land
private double landSpeed_kmh = 50.0; private double landSpeed_kmh = 1.0;
private double waterSpeed_kmh = 8.0; Since they both private double waterSpeed_kmh = 60.0;
private double kmTravelled = 0.0; implement Move private double kmTravelled = 0.0;
interface, we can treat
public Dog(String region) { them as a Move type. public Dolphin(String region) {
this.region = region; this.region = region;
} }
public classpublic
MovingAnimals {
void move(double hours) { public void move(double hours) {
public static void main(String[] args) { if(region.equals("water"))
if(region.equals("water"))
Dog dog = new Dog("land");
kmTravelled += (waterSpeed_kmh *hours); kmTravelled += (waterSpeed_kmh * hours);
Dolphin dolphin = new Dolphin("land");
else if(region.equals("land"))
Move[] movingAnimals = {dog, dolphin};
> java MovingAnimals
else if(region.equals("land")) {
kmTravelled += (landSpeed_kmh * hours); kmTravelled += (landSpeed_kmh * hours);
} m : movingAnimals) {
for(Move
50.0 }
m.move(1.0); 1.0
They both have a similar implementation but We
public double getKMTravelled() {
can then see that
} public double getKMTravelled() {
their land and<program
water movement speed is move() has changed an
return kmTravelled;
System.out.println(dog.getKMTravelled());
end>
return kmTravelled;
different. We could change it }completely internal travelled variable.
}
System.out.println(dolphin.getKMTravelled());
} between the two implementations.
}
}
}
Just like abstract classes we can represent an interface within UML however it
is slightly different than others. ClassName
Attribute
We specify the stereotype
Methods
in UML to be interface and
this gives us specificity of
language constructs.
Just like abstract classes we can represent an interface within UML however it
is slightly different than others.
However! The relationship link is different than that of a classes.
implements
extends
Just like abstract classes we can represent an interface within UML however it
is slightly different than others.
However! The relationship link is different than that of a classes.
Just like abstract classes we can represent an interface within UML however it
is slightly different than others.
However! The relationship link is different than that of a classes.
Simply we are able to define a default method by using the default keyword.
Syntax:
[modifier] default <returntype> MethodName([parameters])
Example:
interface Talk {
public void talk();
public default void talking(){
System.out.println(“I am talking.”);
}
}
public class Alien implements Talk { public class Cat implements Talk {
interface Talk {
public void talk();
public default void talking(){
System.out.println(“I am talking.”);
}
}
public class Alien implements Talk { public class Cat implements Talk {