Object-Oriented Programming, Part 2 • Packages • Information hiding • Access modifiers n private n protected n public n "friendly"

OOP: Object-Oriented Programming, Part 2 1 Packages in Java • A package is a collection of classes (a library).

• In a file the first line must specify the package, e.g., n package mypackage;

• Characteristic of a package n Organized in a hierarchy u Uses the file system for implementing the hierarchy u A package corresponds to a directory n Every package is a name space • By default, classes belong to the unnamed package. • Packages introduce new rules.

OOP: Object-Oriented Programming, Part 2 2 Packages in Java, Cont • Typical problems with packages n Tied to local directory structure n Case sensitive n Default package in current directory.

OOP: Object-Oriented Programming, Part 2 3 Package Example

package com.mycompany.misc; // file Car.java

public class Car { public Car(){ System.out.println ("com.mycompany.misc.Car"); } }

package com.mycompany.misc; // file Truck.java

public class Truck { public Truck(){ System.out.println ("com.mycompany.misc.Truck"); } }

OOP: Object-Oriented Programming, Part 2 4 Accessing Classes in Package • A class myClass in a package mypackage is accessed via n mypackage.myClass • This can be nested to any level n mypackage1.mypackage2.mypackage3.myOtherClass • To avoid too much doting packages can be imported, e.g., n In a file import mypackage1.mypackage2.mypackage3.*, then, myOtherClass does not have to be qualified.

• If name clashes, i.e., same class name in two imported packages, then use . • The package java.lang is always imported.

OOP: Object-Oriented Programming, Part 2 5 Accessing Classes, Example

import com.mycompany.misc.*; import java.math.*;

public class Garage { from com.mycompany.misc Car car1; Truck truck1; public Garage(){ car1 = new Car(); k truck1 = new Truck(); }

public void toPrint(){ System.out.println ("A garage: " + PI); }

}

from java.math

OOP: Object-Oriented Programming, Part 2 6 CLASSPATH • Store misc package in /user/torp/java/com/mycompany/misc directory, i.e., the files Car.class and Truck.class.

• CLASSPATH = /user/torp/java;

• Compiler starts search at CLASSPATH

OOP: Object-Oriented Programming, Part 2 7 Information Hiding • Separate interface from implementation

• How much should a user of a class see? • Rules of thumb n Make instance variables private n Make part of the methods public

OOP: Object-Oriented Programming, Part 2 8 Access Modifiers on Variables/Methods • private n Variable/method is private to the class. n "Visible to my self". • protected n Variable/method can be seen by the class, all subclasses, and other classes in the same package. n "Visible to the family" or "beware of dog". • public n Variable/method can be seen by all classes. n "Visible to all".

OOP: Object-Oriented Programming, Part 2 9 "Friendly" • Default access, has no keyword.

• public to other members of the same package. • private to anyone outside the package.

• Also called package access.

OOP: Object-Oriented Programming, Part 2 10 Public/"Friendly" Example

package com.mycompany.misc;

public class Car { public Car(){ System.out.println ("com.mycompany.misc.Car"); } void foo () { System.out.println ("foo"); } }

// in another package import com.mycompany.misc.*;

public static void main (String args[]){ Car car1 = new Car(); car1.foo; // compile error "private" in this package }

OOP: Object-Oriented Programming, Part 2 11 Private Example

class Singleton { private int myData = 42; private static Singleton s = new Singleton();

// make default constructor private private Singleton(){ } static Singleton getSingleton () { return s; } void singletonMethod() { /* do stuff */ }; int getSingletonData { return myData } }

public class UseSingleton { public static void main (String args[]){ Singleton s1 = new Singleton(); // compile error Singleton s2 = getSingleton(); s2.singletonMethod(); }

OOP: Object-Oriented Programming, Part 2 12 Singleton Design Pattern

Singleton

static getInstance() return uniqueInstance; singletonMethod() getSingletonData()

static uniqueInstance otherData

• Controlled access to one instance • Reduced name space (not a "global" variable) • Permits refinement of operations and representation • Permits a variable number of instances • More flexible than static methods

OOP: Object-Oriented Programming, Part 2 13 Access Modifiers on Classes • private n Not supported in Java! n Default see the slide on friendly • protected n Not supported in Java! • public n Can be seen by all other classes in other packages n Only one public class per file • "Friendly" n A class without an access modified can from classes within the same package. • There are no access modifiers on packages n What would it mean?

OOP: Object-Oriented Programming, Part 2 14 Tools • The Java Development Kit (JDK) contains a set of tools useful for Java programmers. • javac n The Java compiler n Translates Java source code to byte code n javac [options] • java n The Java interpreter n Interprets Java byte code n java [options] • javadoc n The Java documentation generator n javadoc [options] |

OOP: Object-Oriented Programming, Part 2 15 Javadoc, Example

import com.mycompany.misc.*;

/** Implements a garage consisting of cars and trucks */ public class Garage { /** Comment on instance variable */ Car car1; Truck truck1;

/** Default constructor, create a car and a truck */ public Garage(){ /** Comment on method internals */ car1 = new Car(); truck1 = new Truck(); }

/** Prints the vehicles made in the constructor @see Garage#Garage() */ public void toPrint(){ System.out.println ("A garage: " + car1 + truck1); } }

OOP: Object-Oriented Programming, Part 2 16 Class Properties • A class variable is a variable that is common to all instances of the same class. • A class method is a method that operates on a class as it was an object.

• Classes are objects (metaobjects) n Class variables are stored in metaobjects n Java supports metaobject via the class Class. Further there is a set of classes in the package java.lang.reflect. See Chapter 12 "Run- Time Type Identification".n

OOP: Object-Oriented Programming, Part 2 17 Class Properties, cont. • Variables marked with static are class variables. n public static float pi = 3.14;

• Methods marked with static are class methods n public static void main (String args[]){}

• The Math class consists entirely of static methods and variables. n We never construct a Math object. n In general that is not a good design.

OOP: Object-Oriented Programming, Part 2 18 Summary • Package, the library unit in Java. • Access modifiers n Tells clients what they can and cannot. • Separation of interface from implementation. n Very important in design (and implementation). • Guideline: Make elements as hidden as possible. • Object-oriented design hard parts n Decomposing system into objects. n Defining the public interface of the objects. n Finding out what is likely to change. n Finding out what is likely to stay the same.

OOP: Object-Oriented Programming, Part 2 19