Chapter 3 - Objects and classes. This chapter gives an introduction to the main Java and OOP concepts but does not require their full understanding. In terms of grasping the big picture, each student proceeds at his or her own pace. The same concepts are covered later in more depth. Here students get a general idea of how things are put together, get a feel for OOP, and work on exercises that require rearranging small pieces of code.
Trang 1TNT NS Java Methods
METHODS
Object-Oriented Programming
and Data Structures
2nd AP edition with GridWorld
Objects and Classes
Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All
rights reserved.
Trang 2Learn about the general structure of a class, its fields, constructors, and methods
Get a feel for how objects are created and how to call their methods
Learn a little about inheritance in OOP
3-2
Trang 3
OOP
e An OO program models the application as a world of interacting objects
e An object can create other objects
e An object can call another object’s (and Its own) methods (that Is, “send messages’)
e An object has data fields, which hold values that can change while the program Is running
3-3
Trang 4
Objects
e Can model real-world objects
e Can represent GUI (Graphical User Interface) components
e Can represent software entities (events, files, Images, etc.)
e Can represent abstract concepts (for example, rules of agame, a location ona grid, etc.)
3-4
Trang 5Objects in the Bug Runner program
GridWorld = | ~~ @ Griaworta [.JIp]k
WI n d OW World Location Help
Le on a grid location to construct or manipulate at |
Trang 6Classes and Objects
° A class IS a plece of the program’s source code that describes a particular type of
objects OO programmers write class definitions
e An object is called an instance of a class
A program can create and use more than one object (instance) of the same class
Trang 7
Class
e A blueprint for objects ofa particular type
structure (number, types) of the
attributes
e Defines available behaviors of Its
Trang 8Class: Car Object: a car
hs
R10 10001719 TA ,
« *, `
? aoe _—
o RB)
String model model = "Mustang"
int numPassengers numPassengers = 0
double amountOfGas amountOfGas = 16.5
Add/remove a passenger | <« e Get the tank filled
Report when out of gas
Trang 9Class vs Object
e A piece of the program's source e Anentity ina running program
code
(oy the main method
or a constructor or another method)
Trang 10
Class vs Object
same for all of its objects
Trang 11
Bug
the grid Grid
Moves Location
Acts: moves If it can,
Trang 12
Classes and Source Files
e Each class Is stored in a separate file
e The name of the file must be the same as the
name of the class, with the extension java
public class Car 4} name of a class
{ | (and its source file)
Trang 14Import
e Full library class names include the package name For example:
java.awt.Color javax.swing.JButton
let you refer to library classes by their short
import javax.swing.JButton; name JButton go = new JButton("Go");
Trang 15Import (cont'd)
e You can import names for all the classes ina package by using a wildcard *:
Import javax.swing.*; and swing packages
Trang 16import (cont'd)
¢ GridWorld has Its own library (gridworld.jar)
e You need to tell the compiler where to find GridWorld classes:
Trang 17
⁄ SomeClass.java \_
public class SomeClass ——\ Class header
se Fields ——— object’s state; can hold numbers,
characters, strings, other objects
Procedures for constructing
° Constructors a new object of this class
and initializing its fields
Actions that an object
\ (behaviors)
Trang 18
public class Actor
Ẳ private Grid<Actor> grid; Fields private Location location;
private int direction;
private Color color;
Trang 19
Fields
e A.k.a instance variables
° Constitute “private memory” of an object
e Each field has a data type (int, double, String, Color, Location, etc.)
e Each field has a name given by the programmer
Trang 20You name it!
means the field is
shared by all May be present:
objects in the class means the field
is a constant
private Location location;
Trang 21e Always have the same name as the class
e Initialize the object’s fields
e May take parameters
e Aclass may have several constructors that differ in the number and/or types of their
parameters
3-2]
Trang 22Constructors (cont'd)
public class Location <«
Ẳ
private int row;
private int col;
" y public Location (int r, int c)
Trang 23Constructors (cont'd)
An object is created with // BugR geet Java [ i the new operator
Location loc = new Location(3, 5);
Trang 24JButton(String text, Icon icon)
Creates a button with initial text and an icon
Trang 25Methods
e Call them for a particular object:
ActorWorld world = new ActorWorld();
Bug bob = new Bug0);
bob.move( );
bob.turn( );
Trang 26Methods (cont'd)
e The number and types of parameters (a.k.a arguments) passed to a method must match method’s parameters:
public void drawString ( String msg, int x, inty )
g.drawString ("Welcome", 120, 50):
Trang 27
Methods (cont'd)
e A method can return a value to the caller
e The keyword void in the method’s header indicates that the method does not return any value
public void drawString ( )
Trang 28Encapsulation and Information Hiding
e Aclass interacts with other classes only through constructors and public methods
e Other classes do not need to know the
mechanics (implementation details) of a class
to use It effectively
program maintenance (making changes to the code)
Trang 29
Methods (cont'd)
e Constructors and methods can call other
public and private methods of the same class
¢ Constructors and methods can call only public methods of another class
Trang 30Inheritance
e In OOP a programmer can create a new class
by extending an existing class
Superclass (Base class)
Superclass Subclass
(Derived class)
Trang 32
s can add new fields and methods
s can redefine (override) a method of the Superclass
° musf provide Ifs own constructors, Dbut calls superclass's constructors
e does not have direct access to Its
Superclass’s private fields
3-32
Trang 33public class UTurnBug extends Bug
turn(); turn(); turn(); turn();
// Or: setDirection (getDirection() + 180);
}
Trang 34e What are import statements used for?
e What Is a field? A constructor? A method?
e Which operator is used to construct an object?