Download Java Objects and Classes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java
Objects and Classes
Object Oriented Programming







An OO program models the application as a world of
interacting objects.
A typical Java program creates many objects, which as you
know, interact by invoking methods.
Through these object interactions, a program can carry out
various tasks, such as implementing a GUI, running an
animation, or sending and receiving information over a
network.
Once an object has completed the work for which it was
created, its resources are recycled for use by other objects.
An object can create other objects.
An object can call another object’s (and its own) methods
An object has data fields, which hold values that can change
while the program is running.
3-2
A
class is a piece of the program’s source
code that describes a particular type of
objects.
 A class is a template or blueprint that
describes the behavior/ state of the object.
 An object is called an instance of a class. A
program can create and use more than one
object (instance) of the same class.
3-3
A
blueprint for
objects of a
particular type
 Defines the
structure (number,
types) of the
attributes
 Defines available
behaviors of its
objects
Attributes
Behaviors
3-4
Attributes:
String model
Color color
int numPassengers
double amountOfGas
Attributes:
model = "Mustang"
color = Color.YELLOW
numPassengers = 5
amountOfGas = 16.5
Behaviors:
Add/remove a passenger
Get the tank filled
Report when out of gas
Behaviors:
3-5
A
piece of the
program’s source
code
 Written by a
programmer
 An
entity in a
running program
 Created
when the
program is running
(by the main
method or a
constructor or
another method)
3-6
 Specifies
the
structure (the
number and types)
of its objects’
attributes — the
same for all of its
objects
 Specifies the
possible behaviors
of its objects
 Holds
specific
values of
attributes; these
values can change
while the program
is running
 Behaves
appropriately
when called upon
3-7
 Each
class is stored in a separate file
 The name of the file must be the same as the
name of the class, with the extension .java
Car.java
public class Car
{
...
}
By convention, the name
of a class (and its source
file) always starts with a
capital letter.
(In Java, all names are case-sensitive.)
3-8
 Java
programs are usually not written from
scratch.
 There are hundreds of library classes for all
occasions.
 Library classes are organized into packages. For
example:
java.util — miscellaneous utility classes
java.awt — windowing and graphics toolkit
javax.swing — GUI development package
3-9
Objects and Classes
Programmers implement classes
Classes are templates or blueprints for Objects
Data and methods are defined within Classes
Classes must provide an implementation such that objects created from
those classes behave as those defined in the Object model.
An Object is the instantiation of a class
An object is an Instance of a class
The process of creating an object is called instantiation
The attributes of an object are called instance variables
The methods of an object are called instance methods
In Java, Objects are created using the new keyword:
Employee e1 = new Employee();
Defining Classes
A class definition must have the following:
The keyword "class" followed by the name of the class
The class body
Before the keyword "class" is the optional modifier
"public"
If a class is public, it must be defined within a file which is the same
name as the class with a ".java" extension.
i.e. Classname.java
eg. HelloWorld.java, Account.java, Ledger.java, Transaction.java
most classes are declared public
The class body contains:
Zero or more instance variables
Zero or more methods
Example Class Definition
in Employee.java:
public class Employee
{
String name;
Instance
String dept;
Variables:
int salary;
Date startingDate;
// more variable definitions ...
Methods:
public int getSalary() //accessor method
{
return salary;
}
public int computeHourlyRate() //mutator method
{
// calculate hourly rate from salary
}
//more method definitions
}
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int
startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed; }
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue; }
public void setGear(int newValue) {
gear = newValue; }
public void applyBrake(int decrement) {
speed -= decrement; }
public void speedUp(int increment) {
speed += increment; }
}