You are on page 1of 19

ST JOSEPH UNIVERSITY COLLEGE OF ENGINEERING AND

TECNOLOGY

SUBJECT NAME: OBJECT ORIANTED PROGRAMMING IN JAVA

SUBJECT CODE: 350 CS


TOPICS: a. Access protection in java
b. java utility and code documentation
c. proxy saver
Submitted by:
Name: Wambura S.Mohere
Reg.No. 15171351049
Dept: BSE- Mathematics and Computer Science
Year II
Semester III
Date: 24 Jan 2017

Introduction
Here we are going to look on access protection in java package , within or outside the packages:
classes in the same package can access each other's package-private and protected members,
The java.io package contains all the classes required for input and output
operations,
Also lastly in proxy server networking is discussed, a client connects to the proxy server,
requesting some service, such as a file, connection, web page, or other resource available from a
different server and the proxy server evaluates the request as a way to simplify and control its
complexity. Proxies were invented to add structure and encapsulation to distributed systems.
Today, most proxies are web proxies, facilitating access to content on the World Wide Web and
providing anonymity.

Abstract
A Java package organizes Java classes into namespaces, providing a unique
namespace for each type it contains. Classes in the same package can access each
other's package-private and protected members, Java uses the concept of stream to
make I/O operation fast. The java.io package contains all the classes required for
input and output operations, java utilities Contains the collections framework,
legacy collection classes, event model, date and time facilities, internationalization,
and miscellaneous utility classes (a string tokenize, a random-number generator,
and a bit array). Also In proxy server networking , the proxies were invented to add
structure and encapsulation to distributed systems.

Content
Introduction..i
Abstraction. .ii
Content ii
1. Access protection in java...4
1.1 Class Member Access..5
1.2 Java Access Protection Example..6
2. Utilities in java .10
2.1 utility class .......................................................................10
2.3 package java.util.10
2.3 Java Code example.....11
3. Java - Documentation Comments.....12
3.1 Structure of a Javadoc comment.....12
3.2 Specifications are defined in comment lines...............................................13
3.3 Placement of comments...............................................................................14
4. Proxy sever........................................................................................................14
4.1 Types of proxy saver...16
a. Open proxies
b. Reverse proxies
5. Conclusion ...17

1. Access protection in java


Packages add another dimension to the access control. Java provides many levels of protection to
allow fine-grained control over visibility of the variables and methods within classes, subclasses,
and packages.
Classes and packages are means of encapsulating and containing the name space and scope of
the variables and methods. Packages behave as containers for classes and other subordinate
packages, classes act as containers for data and code.
Class Members Visibility
The Java's smallest unit of abstraction is class. Because of the interplay between the classes and
packages, Java addresses the following four categories of visibility for class members:

Subclasses in same package

Non-subclasses in same package

Subclasses in different packages

Classes that are neither in same package nor in subclasses

The three access modifiersare :

public

private

protected

Provides a variety of ways to produce many levels of access required by these categories, while
the access control mechanism of Java may seem complicated, we can simplify it as follows.
Anything declared as public can be accessed from anywhere.
Anything declared as private can't be seen outside of its class.

1.1Class Member Access

When a member doesn't have an explicit access specification, then it is visible to the subclasses
as well as to the other classes in the same package. This is the default access. And If you want to
allow an element to be seen outside your current package, but only to the classes that subclass
your class directly, then declare that element protected.

This table applies only to the members of classes. A non-nested class has only two possible
access levels i.e., default and public.
When a class is declared as public, then it is accessible by any other code. If a class has default
access, then it can only be accessed by the other code within its same package. When a class is
public, it must be only the public class declared in the file that must have the same name as the
class.

1.2 Java Access Protection Example


Here the upcoming example allows all the combinations of access control modifiers. This
example has two packages and five classes.
Always remember that the classes for the two different packages need to be stored in directories
after their respective packages (in this case pkg1 and pkg2).
The source for the first package defines the three classes i.e., Protection, Derived, and
SamePackage. The first class defines the four variables of type int in each of the legal protection
modes. The variable n declared with the default protection, the variables n_priv, n_prot, and
n_publ is private, protected, and public respectively.
6

Each subsequent class in the following example will try to access the variables in an instance of
this class. The lines that will not compile due to the access restrictions are commented out.
Before each of these lines is a comment that listing the places from which this slevel of
protection would allow access.
The second class named Derived, is a subclass of Protection in the same package, pkg1. This
grants Derived access to every variable in the class Protection except for n_priv, the private
one. The third class named SamePackage, is not a subclass of the class Protection, but is in the
same package and also has access to all but not n_priv.

This is Protection.java file:

package pkg1;
public class Protection
{
int n = 1;
private intn_priv = 2;
protected intn_prot = 3;
public intn_publ = 4;
public Protection()
{
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_priv = " + n_priv);
System.out.println("n_prot = " + n_prot);
System.out.println("n_publ = " + n_publ);
}
}

This is Derived.java file:

package pkg1;
class Derived extends Protection
{
Derived()
{
System.out.println("derived constructor");
System.out.println("n = " + n);
7

/* class only
* System.out.println("n_priv = "4 + n_priv); */
System.out.println("n_prot = " + n_prot);
System.out.println("n_publ = " +n_publ);s
}
}

This is SamePackage.java file:

package pkg1;
class SamePackage
{
SamePackage()
{
Protection pro = new Protection();
System.out.println("same package constructor");
System.out.println("n = " + pro.n);
/* class only
* System.out.println("n_priv = " + pro.n_priv); */
System.out.println("n_prot = " + pro.n_prot);
System.out.println("n_publ = " + pro.n_publ);
}
}

Following is the source code for the other package named pkg2. The two classes defined in the
package pkg2 cover the outer two conditions that are affected by the access control. The first
class named Protection2, is a subclass of pkg1.Protection. This grants access to all of pkg1.
Variables of the class Protection except for n_priv (because it is private) and n, the variable
declared with the default protection.
Always remember that the default only allows access from within the class or the package, not
extra-package subclasses. Finally, the class OtherPackage has access to n_publ only which was
declared as public.

This is Protection2.java file:

package pkg2;
class Protection2 extends pkg1.Protection
{
Protection2()
8

{
System.out.println("derived other package constructor");
/* class or package only
* System.out.println("n = " + n); */
/* class only
* System.out.println("n_priv = " + n_priv); */
System.out.println("n_prot = " + n_prot);
System.out.println("n_publ = " + n_publ);
}
}

This is OtherPackage.java file:

package pkg2;
class OtherPackage
{
OtherPackage()
{
pkg1.Protection pro = new pkg1.Protection();
System.out.println("other package constructor");
/* class or package only
* System.out.println("n = " + pro.n); */
/* class only
* System.out.println("n_priv = " + pro.n_priv); */
/* class, subclass or package only
* System.out.println("n_prot = " + pro.n_prot); */
System.out.println("n_publ = " + pro.n_publ);
}
}

2. Utilities in java
2.1 The package java.util
It contains a number of useful classes and interfaces. Although the name of the package might
imply that these are utility classes, they are really more important than that. In fact, Java depends
directly on several of the classes in this package, and many programs will find these classes
indispensable. The classes and interfaces in java.util include:

The Hashtable class for implementing hashtables, or associative arrays.

The Vector class, which supports variable-length arrays.

The Enumeration interface for iterating through a collection of elements.

The StringTokenizer class for parsing strings into distinct tokens separated by delimiter
characters.

The EventObject class and the EventListener interface.

The Locale class in Java 1.1, which represents a particular locale for internationalization
purposes.

The Calendar and TimeZone classes in Java. These classes interpret the value of a Date
object in the context of a particular calendar system.

The ResourceBundle class and its subclasses, ListResourceBundle and


PropertyResourceBundle.

2.2 utility class

10

In computer programming, a utility class is a class that defines a set of methods that perform
common, often re-used functions. Most utility classes define these common methods under static
(see Static variable) scope. Examples of utility classes include
java.util.Collections, which provides several utility methods (such as sorting) on objects that
implement a Collection (java.util.Collection.
Class Collections
java.lang.Object
java.util.Collections
public class Collections
extends Object
This class consists exclusively of static methods that operate on or return collections. It contains
polymorphic algorithms that operate on collections, "wrappers", which return a new collection
backed by a specified collection, and a few other odds and ends.
The methods of this class all throw a NullPointerException if the collections or class objects
provided to them are null.
The Collections utility class consists exclusively of static methods that operate on or return
collections. It contains polymorphic algorithms that operate on collections, "wrappers", which
return a new collection backed by a specified collection,
Some useful method in Collections class:
Method Signature
Collections.sort(List myList)

Collections.sort(List, comparator c)
Collections.shuffle(List myList)
Collections.reverse(List myList)
Collections.binarySearch(List mlist, T key)
Collections.copy(List dest, List src)

Description
Sort the myList (implementation of any List
interface) provided an argument in natural
ordering.
Sort the myList(implementation of any List
interface) as per comparator c ordering (c class
should implement comparator interface)
Puts the elements of myList ((implementation
of any List interface)in random order
Reverses the elements of myList
((implementation of any List interface)
Searches the mlist (implementation of any List
interface) for the specified object using the
binary search algorithm.
Copy the source List into dest List.

11

Let's take the example of List sorting using Collection class. We can sort any Collection using
Collections utility class. i.e.; ArrayList of Strings can be sorted alphabetically using this utility
class. ArrayList class itself is not providing any methods to sort. We use Collections class static
methods to do this. Below program shows use of reverse(), shuffle(), frequency() methods as
well.
2.3 Java Code:
1. package utility;
2.
3. import java.util.Collections;
4. import java.util.ArrayList;
5. import java.util.List;
6.
7. public class CollectionsDemo {
8.
9.

public static void main(String[] args) {

10.

List<String>student<String>List = new ArrayList();

11.

studentList.add("Neeraj");

12.

studentList.add("Mahesh");

13.

studentList.add("Armaan");

14.

studentList.add("Preeti");

15.

studentList.add("Sanjay");

16.

studentList.add("Neeraj");

17.

studentList.add("Zahir");

18.
19.

System.out.println("Original List " + studentList);

20.
12

21.

Collections.sort(studentList);

22.

System.out.println("Sorted alphabetically List " + studentList);

23.
24.

Collections.reverse(studentList);

25.

System.out.println("Reverse List " + studentList);

26.

Collections.shuffle(studentList);

27.

System.out.println("Shuffled List " + studentList);

28.

System.out.println("Checking occurance of Neeraj: "

29.

+ Collections.frequency(studentList, "Neeraj"));

30.

31.}

3. Java - Documentation Comments


Javadoc (originally cased as JavaDoc)is a documentation generator created by Sun Microsystems
for the Java language (now owned by Oracle Corporation) for generating API documentation in
HTML format from Java source code. The HTML format is used to add the convenience of being
able to hyperlink related documents together.
3.1 Structure of a Javadoc comment
A Javadoc comment is set off from code by standard multi-line comment tags /* and */. The
opening tag (called begin-comment delimiter), has an extra asterisk, as in /**.
1. The first paragraph is a description of the method documented.
2. Following the description are a varying number of descriptive tags, signifying:
1. The parameters of the method (@param)
2. What the method returns (@return)

13

3. Any exceptions the method may throw (@throws)


4. Other less-common tags such as @see (a "see also" tag)
The basic structure of writing document comments is to embed them inside /** ... */. The
Javadoc is written next to the items without any separating newline. Note that any import
statements must precede the class declaration. The class declaration usually contains:

// import statements
/**
* @author
FirstnameLastname<address @ example.com>
* @version 1.6
(current version number of program)
* @since
1.2
(the version of the package this class was first added to)
*/
publicclassTest{
// class body
}

The Java language supports three types of comments


s/n

Comment

Description

/*text*/

The compiler ignores everything from /* to */.

//text

The compiler ignores everything from // to the end of the

/**documentation*/

This is a documentation comment and in general its calleddoc


comment. The JDK javadoc tool uses doc comments when
preparing automatically generated documentation.

3.2 Specifications are defined in comment lines.


/**
14

* This is the typical format of a simple


* documentation comment that spans two lines.
*/
/** This comment takes up only one line. */

3.3 Placement of comments


All comments are placed immediately before class, interface, constructor, method, or field
declarations. No other stuff between them are not permitted.
/**
* This is the class comment for the class Whatever.
*/
import com.sun; // MISTAKE
public class Whatever {}
The section that introduces the class, method of field. It comes first in the specification before
the tag section.
The first sentence of each doc comment should be a summary sentence, containing a concise but
complete description of the declared entity. It will be placed in package overview and class
overview.

c. Proxy sever
In computer networks, a proxy server is a server (a computer system or an application) that acts
as an intermediary for requests from clients seeking resources from other servers. A client
connects to the proxy server, requesting some service, such as a file, connection, web page, or
other resource available from a different server and the proxy server evaluates the request as a
way to simplify and control its complexity. Proxies were invented to add structure and
encapsulation to distributed systems. Today, most proxies are web proxies, facilitating access to
content on the World Wide Web and providing anonymity.

15

A reverse proxy taking requests from the Internet and forwarding them to servers in an internal
network. Those making requests connect to the proxy and may not be aware of the internal
network.
Many schools block access to popular websites such as Face book. Students can use proxy
servers to circumvent this security. However, by connecting to proxy servers, they might be
opening themselves up to danger by passing sensitive information such as personal photos and
passwords through the proxy server. Some content filters block proxy servers in order to keep
users from using them to bypass the filter.

A proxy server may reside on the user's local computer, or at various points between the user's
computer and destination servers on the Internet.
3

A proxy server that passes requests and responses unmodified is usually called a gateway or
sometimes a tunneling proxy.

16

A forward proxy is an Internet-facing proxy used to retrieve from a wide range of sources (in
most cases anywhere on the Internet).

A reverse proxy is usually an internal-facing proxy used as a front-end to control and protect
access to a server on a private network. A reverse proxy commonly also performs tasks such
as load-balancing, authentication, decryption or caching.

Types of proxy saver


5.2

Open proxies

An open proxy is a forwarding proxy server that is accessible by any Internet


user. Gordon Lyon estimates there are "hundreds of thousands" of open
proxies on the Internet.[3] An anonymous open proxy allows users to conceal
their IP address while browsing the Web or using other Internet services.
There are varying degrees of anonymity however, as well as a number of
methods of 'tricking' the client into revealing itself regardless of the proxy
being used

An open proxy forwarding requests from and to anywhere on the Internet

5.3 Reverse proxies


Reverse proxies (or surrogate) is a proxy server that appears to clients to be an ordinary server.
Requests are forwarded to one or more proxy servers which handle the request. The response
from the proxy server is returned as if it came directly from the original server, leaving the client
17

no knowledge of the origin servers. Reverse proxies are installed in the neighborhood of one or
more web servers. All traffic coming from the Internet and with a destination of one of the
neighborhood's web servers goes through the proxy server. The use of "reverse" originates in its
counterpart "forward proxy" since the reverse proxy sits closer to the web server and serves only
a restricted set of websites

A reverse proxy taking requests from the Internet and forwarding them to servers in an internal
network Those making requests connect to the proxy and may not be aware of the internal
network.

Conclusion
Java security technology includes a large set of APIs, tools, and implementations of commonly
used security algorithms, mechanisms, and protocols. The Java security APIs span a wide range
of areas, including cryptography, public key infrastructure, secure communication,
authentication, and access control. Java security technology provides the developer with a
comprehensive security framework for writing applications, and also provides the user or
administrator with a set of tools to securely manage applications.

End

18

19

You might also like