Introduction To Java PP2
Introduction To Java PP2
Classes
The template or blueprint from which
objects are made.
Construct an object from class means to
create an instance of that class
Encapsulation aka info hiding.
The data inside an object is known as its
instance fields
Function that operate on the data are called
methods
Class cont
Notion that classes are
cookie cutters and
objects are the cookies
created from classes
The user interacts with
the methods of class
you should never deal
with how the methods
are implemented
Classes cont
Java has the ability to extend a class.
A new class has all of the properties and
methods of the class you extend
You can write new methods and add
properties that apply to your new class.
This is also referred to as inheritance
Where to start??
The top down approach doesnt apply
You dont start with the void main(String args*+)
Object Variables
To work with objects, you first construct them and specify their
initial state. Then you apply methods to the objects.
You use constructors to construct new instances. A constructor is a
special method whose purpose is to construct and initialize objects.
Constructors always have the same name as the class name. To
construct an object, you combine the constructor with
the new operator.
new Date();
Verbs?
Bike (behaviors)
Bicycle Class
Bike properties
Gear, speed, and
cadence
State
Current gear, speed,
and cadence
Methods
SpeedUp, ChangeGear,
Brake, ChangeCadence
+speed+" gear:"+gear);
Extending Bicycle
class MountainBike extends Bicycle {
// new fields and methods defining a mountain bike would go here
Constructors
Constructors Cont
this
Instance Fields
Initializing Fields
Method Parameters
Packages
Java allows you to group classes in a collection
called a package.
Packages are convenient for organizing your
work
separating your work from code libraries provided
by others
Packages Cont.
Static Imports
Extending A Class
Dynamic Binding
super
Whenever you want to access a instance field
of your super class inside of your sub class you
have to use the keyword super
See eclipse for example
Final Classes
Final Methods