Code 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 ME 135 - Spring 2005 - Java object/syntax intro - 1
Packages • 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". ME 135 - Spring 2005 - Java object/syntax intro - 2
Code 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(System.in)); while(true){ try { String s = input.readLine(); System.out.println("Input: " + s); } catch(Exception e){} } } } ME 135 - Spring 2005 - Java object/syntax intro - 3
Exception 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). ME 135 - Spring 2005 - Java object/syntax intro - 4
Exception handling example public class Except { public static double rnd() throws Exception { double d = java.lang.Math.random(); // Alt. to import if(d < 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/notcaught); } } } } ME 135 - Spring 2005 - Java object/syntax intro - 5
Aggregation • 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 ME 135 - Spring 2005 - Java object/syntax intro - 6
Aggregation 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); } } ME 135 - Spring 2005 - Java object/syntax intro - 7
Composition • One class "has" another • Distinct from aggregation because the lifetime of the two classes are the same. ME 135 - Spring 2005 - Java object/syntax intro - 8
Composition 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……. ME 135 - Spring 2005 - Java object/syntax intro - 9
Composition 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); } } ME 135 - Spring 2005 - Java object/syntax intro - 10
Inheritance • 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. ME 135 - Spring 2005 - Java object/syntax intro - 11
Inheritance 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()); } } ME 135 - Spring 2005 - Java object/syntax intro - 12
Another inheritance example See this Java file.... ME 135 - Spring 2005 - Java object/syntax intro - 13
UML 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 ME 135 - Spring 2005 - Java object/syntax intro - 14
Choosing between composition and inheritance • The default should be composition or aggregation • Inheritance used only when there is a clear "is-a" relationship. ME 135 - Spring 2005 - Java object/syntax intro - 15