Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Defining your Own Classes

Object Oriented Programming


Syntax of Class Definitions
Syntax of Method Definitions
The return statement
The void return type
Object Oriented "Hello World" example
Classes for Testing
Object Oriented Programming
Communicating Object
 A running program is a collection of
objects that are each doing their own
task and communicating with other
objects. The picture shows a running
program (or a pizza shop). The
jagged lines represent
communication. The boss is special
and so is drawn with dotted lines.

 In Java programs the main() method


is special because it is where the Java
interpreter starts the whole program
running. The main() method is
a static method, which means that
there will be only one instance of it
and that it exists (as part of a class)
before any objects have been created.
More Object created at Run Time
 Object-oriented programs can
create more objects as the
program runs. The new objects
might be needed to handle
additional data that the program
needs to work with. The picture
shows your business after a few
more employees have been
added.
 Notice that objects (workers)
can communicate with each
other, not just with main() (the
boss). Some objects do not
communicate with main() at all,
but communicate only with
other objects in the system.
Syntax of a Class Definition
Class definitions look like this:
class ClassName
{
Descriptions of the instance variables and methods each object will have and the constructors that
initialize a new object.
}
Often programmers separate the definition into three sections:
class ClassName
{
// Description of the variables.
// Description of the constructors.
// Description of the methods.
}

Separating the class into sections is done for clarity. It is not a rule of the language. A
simple class might have just a few variables and be defined in just a few lines of code. A
large, complicated class might take thousands of lines of code for its definition.
A Tiny Example

Program which includes two class definitions.


The definition of class HelloObject includes a method but no instance variables, so objects of
class HelloObject have no instance variables. The class does have a constructor but it is not
explicitly defined in the code.
The definition of class HelloTester contains only the static main() method. When
the main() method starts, it constructs a HelloObject and then invokes that
object's speak() method.
Remember that name of the file must match the name of the class that contains
the main() method.
Question
 What two classes are defined in this code?
 What method is in the first class?
Method Definition
Method definitions look like this:
returnType methodName( parameterList )
{
// Java statements
return returnValue;
}

The returnType is the type of value that the method hands back to the caller of
the method. Methods in classes you define can return values just as do methods from
library classes. The return statement is used to hand back a value to the caller.
If you want a method that does something, but does not return a value to the caller, use a
return type of void and do not use a return value with the return statement.
The return statement can be omitted; the method will automatically return to the caller
after it executes. Here is the method from the example program:
// method definition
void speak() {
System.out.println("Hello from an object!");
}

You might also like