Top Banner
11. Reference Organization Packages
21

11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Jan 19, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

11. Reference Organization

Packages

Page 2: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

What is reference organization?

• It is a process that enables us to group classes and interfaces into isolated namespaces.

Page 3: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

What is an isolated namespace?

• A name is an identifier.

• A namespace is place where identifiers are grouped.

• An isolated namespace is an isolated identifier group.

Page 4: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why isolate identifier groups?

• To enable the reuse of identifiers w/o causing confusion.

• For example:– import java.util.List;– import java.awt.List;– ....– List l = new List(); – // this is an error, how do I fix it?

Page 5: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Ambiguous class name conflict...

• java.awt.List l = new java.awt.List();

• java.util.List ll = new java.util.List();

Page 6: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why not just use unique identifiers?

• Because we can’t control all the programmers who write libraries that we want to use.

Page 7: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

What is a package?

• Isolated grouping of reference identifiers (classes and interfaces)

Page 8: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

How do I create a package?

reserved word “package” appears as the first java statement in a file.

It appears only once.

ex:

package com.docjava.futil;

public class Foo {}

Page 9: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Packages are ALWAYS public!

• private package foo; // syntax error

• public package foo; // syntax error

• protected package foo; //syntax error

• package foo; // OK.

Page 10: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Visibilty

• Public – world access

• Protected – sub-class access

• Default – package access

• Private – only within the class.

Page 11: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

ReadOnly Variables

public class Customer {

private String name = “J. Shmoe”;

public String getName() {

return name;

}

}

Page 12: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why do I need public?

• Publish a constant:– public static final double PI =

3.141592653589626

• Publish a method – public static double sin(double x){…}

publish a class

- allows others to use your API!

Page 13: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why do I need protected?

• You cannot have a protected class.

• You can have protected variables and methods.

• Only class authors can use protected items.

• Sub-Class authors know what they are doing!??!? Trust them!?!?!

Page 14: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why do I need private?

• Don't trust the subclass author!

• Some things can be read but not written.

• Some things can be written but not read.

• I can control access to state variables.

Page 15: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Read-only variable

public class Taxes {private int interestRate = 3;

public getInterestRate() {// accessor

return interestRate;

}

public void greenSpanManeuver() {

interestRate = interestRate + 1;

}

}

Page 16: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Write-only Variable

public class Taxes {private int interestRate = 3;

public void setInterestRate(int r) {// mutator

interestRate = r;

}

public void greenSpanManeuver() {

interestRate = interestRate + 1;

}

}

Page 17: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Why do I need private methods?

• you don't want to publish the method!

• Perhaps the spec will change.

• public stuff must be stable

• public stuff must be supported

• public stuff must be documented

Page 18: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Be careful what you make public

• public interface car {– public int getSpeed();– public int getOdometer();– public int getGas();– public void setAcceleration(int a);– public void setSteerWheelAngle(int degrees);

• }

Page 19: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Default is easy!

• do nothing, it is default!

• Why not make everything default?– No one can use your stuff!– Your stuff is unpublished!– complexity in an OOD is a function of class

interdependence. – You must encapsulate complexity!

Page 20: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

How do I share protected elements?

package dl;

public class ShareMe {

protected int i = 10;

}

Page 21: 11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.

Using the protected member

package sop;

class SomeClass extends dl.ShareMe {

SomeClass() {

System.out.println(i);

}

}