Sie sind auf Seite 1von 19

Object-Oriented Programming, Part 2

Packages Information hiding Access modifiers


n n n n

private protected public "friendly"

OOP: Object-Oriented Programming, Part 2

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 u

Uses the file system for implementing the hierarchy A package corresponds to a directory

Every package is a name space

By default, classes belong to the unnamed package. Packages introduce new scope rules.
OOP: Object-Oriented Programming, Part 2 2

Packages in Java, Cont


Typical problems with packages
n n n

Tied to local directory structure Case sensitive Default package in current directory.

OOP: Object-Oriented Programming, Part 2

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

Accessing Classes in Package


A class myClass in a package mypackage is accessed via
n

mypackage.myClass mypackage1.mypackage2.mypackage3.myOtherClass In a file import mypackage1.mypackage2.mypackage3.*, then, myOtherClass does not have to be qualified.

This can be nested to any level


n

To avoid too much doting packages can be imported, e.g.,


n

If name clashes, i.e., same class name in two imported


packages, then use fully qualified name. The package java.lang is always imported.

OOP: Object-Oriented Programming, Part 2

Accessing Classes, Example


import com.mycompany.misc.*; import java.math.*; public class Garage { Car car1; Truck truck1; public Garage(){ car1 = new Car(); k truck1 = new Truck(); }

from com.mycompany.misc

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

from java.math

OOP: Object-Oriented Programming, Part 2

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; <etc> Compiler starts search at CLASSPATH

OOP: Object-Oriented Programming, Part 2

Information Hiding
Separate interface from implementation How much should a user of a class see? Rules of thumb
n n

Make instance variables private Make part of the methods public

OOP: Object-Oriented Programming, Part 2

Access Modifiers on Variables/Methods


private
n n

Variable/method is private to the class. "Visible to my self". Variable/method can be seen by the class, all subclasses, and other classes in the same package. "Visible to the family" or "beware of dog". Variable/method can be seen by all classes. "Visible to all".

protected
n n

public
n n

OOP: Object-Oriented Programming, Part 2

"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() singletonMethod() getSingletonData() static uniqueInstance otherData return uniqueInstance;

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
13

OOP: Object-Oriented Programming, Part 2

Access Modifiers on Classes


private
n n

Not supported in Java! Default see the slide on friendly Not supported in Java! Can be seen by all other classes in other packages Only one public class per file A class without an access modified can from classes within the same package. What would it mean?
14

protected
n

public
n n

"Friendly"
n

There are no access modifiers on packages


n OOP: Object-Oriented Programming, Part 2

Tools
The Java Development Kit (JDK) contains a set of tools useful
for Java programmers. javac
n n n

The Java compiler Translates Java source code to byte code javac [options] <filename.java> The Java interpreter Interprets Java byte code java [options] <filename.class> The Java documentation generator javadoc [options] <filename.class> | <package>
15

java
n n n

javadoc
n n

OOP: Object-Oriented Programming, Part 2

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 n

Class variables are stored in metaobjects Java supports metaobject via the class Class. Further there is a set of classes in the package java.lang.reflect. See Chapter 12 "RunTime 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 n

We never construct a Math object. 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. Very important in design (and implementation).

Separation of interface from implementation.


n

Guideline: Make elements as hidden as possible. Object-oriented design hard parts


n n n n

Decomposing system into objects. Defining the public interface of the objects. Finding out what is likely to change. Finding out what is likely to stay the same.

OOP: Object-Oriented Programming, Part 2

19

Das könnte Ihnen auch gefallen