Title: Code reuse
1Code reuse
- Using someone else's classes
- Aggregation and Composition
- Inheritance
- The briefest of Unified Modeling Language (UML)
- Choosing between composition and inheritance
- "Casting" from one type to another
2Packages
- Package is a Java construct for sharing code
- A single package is typically a body of related
code - Allow use of the objects in a package from your
own .java file by using the Java keyword "import".
3Code slide
import java.io. // Makes all of java.io
available public class ConsoleIO public
static void main(String args)
BufferedReader input input new
BufferedReader(new InputStreamReader(S
ystem.in)) while(true) try
String s
input.readLine()
System.out.println("Input " s)
catch(Exception e)
4Exception handling (brief)
- An exception is "thrown" when an exceptional
condition (usually an error condition) is
detected - When you write code using a method from a library
that throws an exception, you must "catch" the
exception (or your code won't compile).
5Exception handling example
public class Except public static double
rnd() throws Exception double d
java.lang.Math.random() // Alt. to import
if(d lt 0.01)throw new Exception()
return d public static void
main(String args) int notcaught 0
int caught 0 while(true)
try rnd()
notcaught
catch(Exception e) caught
System.out.println((float)caught/notca
ught)
6Aggregation
- Aggregation is relevant when someone else has
developed a class that, as it is, does something
useful for you - One class "uses" another
- If the "using" class goes away, the "used" class
does not go away that is, the lifetime of the
used class does not depend on the lifetime of the
using class
7Aggregation example
class Professor Professor(short i)id
i short id class Student
Student(int i, Professor a)advisor a id
i Professor advisor int id public
class Aggregation public static Professor
p public static void main(String args)
p new Professor((short)123)
func() System.out.println("Professor "
p) // Professor still exists here!
private static void func() Student
s1 new Student(23456, p) Student s2
new Student(54321, p)
8Composition
- One class "has" another
- Distinct from aggregation because the lifetime of
the two classes are the same.
9Composition example
class Heart Heart(double m)mass m
private double mass class Head
Head(double m)mass m private double
mass class HumanBody HumanBody(double
headmass, double heartmass) head new
Head(headmass) heart new
Heart(heartmass) private Head head
private Heart heart // Continued next page.
10Composition example
public class Composition public static void
main(String args) func() //
Human body, including heart and head, is gone.
private static void func()
HumanBody body new HumanBody(2.0, 1.0)
System.out.println("Head " body.head.mass
", heart " body.heart.mass)
11Inheritance
- Inheritance is relevant when someone has
developed a class that is a generalized version
of what you need - When one class "inherits" from another, the
inheriting class is said to have an "is-a"
relationship with the inherited class - The inheriting class is often called a subclass
of the inherited class - The inherited class is often called the
superclass of the inheriting class.
12Inheritance example
class Bike int rearGear int
frontGear double gearRatio()return
(double)frontGear / rearGear class
MountainBike extends Bike // Additional
behavior for triple chainrings in front. class
TandemBike extends Bike // Additional
behavior for dual seats and pedals. public
class Inheritance public static void
main(String args) MountainBike
mb new MountainBike() // Subclass uses
the superclass method. System.out.println(
"Gear ratio " mb.gearRatio())
13Another inheritance example
See this Java file....
14UML very briefly
- What is UML, and what is it used for?
- We will use the static diagram and state diagrams
- We will use the static diagram to show
inheritance, composition, aggregation - See pages 4, 5, 6, 7 of Introduction to the UML
15Choosing between composition and inheritance
- The default should be composition or aggregation
- Inheritance used only when there is a clear
"is-a" relationship.
16Add
- Slide on abstract classes
- Slide on interface
17Code slide
public class Composition public static void
main(String args) func() //
Human body, including heart and head, is gone.
private static void func()
HumanBody body new HumanBody(2.0, 1.0)