Java Methods & Classes
Java Methods & Classes
double
char boolean
Built in Classes
Java has a large built in library of classes with lots of useful methods Ones you should become familiar with quickly String Math Integer, Character, Double
System Arrays Scanner File Object Random Look at the Java API page
Encapsulation
Objects hide their functions (methods) and data (instance variables)
Inheritance
Each subclass inherits all variables of its manual superclass
car
Super class
automatic
Subclasses
Polymorphism
Interface same despite draw() different data types
draw()
More Objects
Classes describe the data held by each of its objects Data usually goes first in a class Example:
class Dog { String name; int age; ...rest of the class... } A class may describe any number of objects
Examples: "Fido", 3; "Rover", 5; "Spot", 3;
A class may contain methods that describe the behavior of objects Methods usually go after the data Example:
class Dog { ... void bark() { System.out.println("Woof!"); } }
When we ask a particular Dog to bark, it says Woof! Only Dog objects can bark; the class Dog cannot bark
Example:
System.out.println("Woof!"); This causes the particular Dog to print (actually, display on the screen) the characters Woof!
A method may contain local temporary data that exists only until the method finishes Example:
A constructor is a piece of code that constructs, or creates, a new object of that class If you dont write a constructor, Java defines one for you (behind the scenes) You can write your own constructors Example: (This part is the constructor) class Dog {
String name; int age; Dog(String n, int age) { name = n; this.age = age; } }
Variable Scope
Variables may be declared in: Class state variables Method/constructor local variables (and parameters) Inner block of a method also local variables A variable is recognized throughout the block in which it was defined. This is called the scope of the variable. Local variables are allocated when the method is entered and freed when the method exits. The same name may be used in different scopes, and refer to totally different things If the same name is used in an outer and inner scope, then the inner scope definition hides the outer one.
When appearing inside an instance method, the this keyword denotes a reference to the object that the method is acting upon. The following are equivalent:
public int getHours() { return hours; } public int getHours() { return this.hours; }
Visibility Modifiers
We accomplish encapsulation through the appropriate use of visibility modifiers Visibility modifiers specify which parts of the program may see and use any particular class/method/field Information hiding is good! A modifier is a Java reserved word that specifies particular characteristics of a programming construct We've used the modifier final to define a constant Java has three visibility modifiers: public, private, and protected We will discuss the protected modifier later in the course
Public/private
Methods/data may be declared public or private meaning they may or may not be accessed by code in other classes Good practice:
keep data private keep most methods private
public yes
protected yes
default yes
private no
yes
no
no
Using objects
Here, code in one class creates an instance of another class and does something with it
Fruit plum=new Fruit(); int cals; cals = plum.total_calories();
Dot operator allows you to access (public) data/methods inside Fruit class
API Documentation
Your classes are often intended to be used by other programmers Programmers that use your class are not interested in the way it is implemented. They want to use it as a whole and are only interested in what it does and how to use it. API (Application Programmer Interface) documentation is a description of the interface of the class intended for the application programmer who wants to use it. To use the class, we need not (and should not) look at the code. All that is needed is the class API.
API Documentation
The JDK contains a special tool for the generation of API documentation for your classes, called javadoc. Any documentation which is part of the interface begins with /** (double asterick) and ends with */ javadoc takes as input Java programs and automatically generates documentation using: the public/protected method signatures the documentation comments (enclosed by /** */). The output is an HTML file which can be viewed by an internet browser.
Clock API Documentation /** cont. a Clock: Sets the clock to 12:00:00 * Constructs
*/ public Clock(){ this(12,0,0); }
Javadoc Process
.java file
javadoc
.html file
View using a
browser
You should put a documentation comment for the class itself and for any member of the class which is part of its interface. All public constructors and methods should have documentation comments. Private methods are not part of the interface of the class, thus javadoc skips them. (But you should still comment them for internal purposes.) In the rare cases that there are public fields, they should have documentation comments.
API Documentation
Remember that documentation comments are written for programmers who use your class as a whole. They should describe only What the class does, How to use it. Documentation comments should not describe how a class is implemented. Documentation comments should be Short and descriptive, Written in a simple language (ENGLISH), Accurate. Assume that the reader doesnt know anything about your class
Documentation comments can also include tagged paragraphs that give a standard way to document several features of the interface such as method parameters, return values, etc. A tagged paragraph begins with the symbol @ followed with a tag keywords. Tags: @see, @author, @version, @param, @return, @exception.
@param tag
/** * Changes the current time to hour:minute:second * @param hours The new hour value. * @param minutes The new minutes value. * @param seconds The new seconds value. */ public void setTime(int hours, int minutes, int seconds) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; }
@return
/** * Returns the current hour * @return The current hour (between 1 and 12). */ public void getHour() {
return hours; }
getHour javadoc
javap - to print the Java bytecodes javaprof - Java profiler javadoc - documentation generator javah - creates C header files
Stream Manipulation
Example:
Open a file "myfile.txt" for reading FileInputStream fis = new FileInputStream("myfile.txt"); Open a file "outfile.txt" for writing FileOutputStream fos = new FileOutputStream ("myfile.txt");
35
36
Filters
Once a stream (e.g., file) has been opened, we can attach filters Filters make reading/writing more efficient Most popular filters: For basic types: DataInputStream, DataOutputStream For objects: ObjectInputStream, ObjectOutputStream
37
38
Object serialization
Write objects to a file, instead of writing primitive types.
Use the ObjectInputStream, ObjectOutputStream classes, the same way that filters are used.
40
42