Download Introduction to Classes and Object Oriented Programming - Slides | CS 140 and more Study notes Computer Science in PDF only on Docsity!
Introduction to Classes and Object OrientedIntroduction to Classes and Object Oriented^ Introduction to Classes and Object OrientedProgrammingProgrammingProgramming
Overview^ OverviewIn these chapters you will learn:^
What classes, objects, methods and instancevariables are.
How to declare a class and use it to create an object.
How to declare methods in a class to implement theclass’s behaviors.
How to declare instance variables in a class toimplement the class’s attributes.
How to call an object’s method to make that methodperform its task.
The differences between instance variables of a classand local variables of a method.
How to use a constructor to ensure that an object’sdata is initialized when the object is created.
Object Oriented Programming^ Object Oriented Programming^
Before Object Oriented Programming:^
Applications were made of procedures that call eachother.
The application is broken down into several smaller tasksand each task was implemented as a procedure (orsometimes called a function). ^
That’s why this approach is called “ProceduralProgramming”
There used to be a main procedure that starts theapplication and links several procedure together.
In summary, in Procedural programming, the unitfor biulding an application is a “Procedure” or afunction.
In Object Oriented Programming, the focus is onthe objects that exist in the application domain:^
What is each object made of?
How the objects interact with each other.
Objects and Object Oriented Programming^ Objects and Object Oriented Programming^
In object oriented programming, objects areprogram constructs (or units) that are used torepresent things within the software application.^
Examples: person, car, air fan, bank account, etc.
Objects have information about themselves.^
For example, a person knows his name (data) and knowshow to walk and how to talk (behavior).
An object oriented application will have differentobjects that interact with each other.
How can the application create these object?
Real-^ Real
-Life ObjectsLife Objects
The following is a picture of an air fan. Can you describe it?
Please, make your description as general as possible so that itcan be applied to other air fans as well.
Your descriptions should cover the looks as well asfunctionality of the fan.
Real-^ Real
-Life ObjectsLife Objects
Does your description apply to the following fans?
How about these??!!
Creating Classes^ Creating Classes^
Classes are abstract data types just as theone we did for the air fan
In Java, classes are defined using a classdeclaration:
[public]
class
ClassName
class-body
Generally a class will have^
Variables (data)
Methods (behavior)
Creating Classes^ Creating Classes^
A public class, must be written in aseparate source file that has its name.
Example: Create a class AirFan thatsimulates an air fan and use it in a smallprogram.
Using Classes to Create Program Objects^ Using Classes to Create Program Objects
Instantiation
Object
classname objectname
= new
classname
( agrs
Scanner input = new Scanner(System.in);
Examples:
Class^ AirFan MyDeskFan = new AirFan();
Using Classes to Create Program Objects^ Using Classes to Create Program Objects
Class
Applications use classes to create objects in memory
public
class
AirFan
{
int
numOfBlades; String
color;
boolean
powerOn;
int
speed; boolean
pluggedIn;
............... }
AirFan
MyBedRoomFan
=^
new
AirFan();
AirFan
MyDeskFan
(^) = new
AirFan();
numOfBlades
colorpoerOnspeed MyDeskFan pluggedIn numofBlades
colorpoerOnspeed MyBedRoomFan pluggedIn
Using the AirFan^ Using the
AirFan Class
Class
public
class
TestFan
{
public
static
void
main(String[]
args)
{
//^
Create
one
object
of
the
AirFan
class
AirFan
myDeskFan
=^
new
AirFan();
myDeskFan.initializeFan();myDeskFan.dispStatus();//^
the
following
steps
will
simulate
the
following:
//^
1-^
Plug
in
the
power
for
first
fan
//^
2-^
turn
it
on
//^
3-^
increase
the
speed
//^
4-^
unplug
the
power
while
it
is
on
myDeskFan.plugInPower();myDeskFan.dispStatus();myDeskFan.turnOn();myDeskFan.dispStatus();myDeskFan.increaseSpeed();myDeskFan.dispStatus();myDeskFan.unPlugPower();myDeskFan.dispStatus(); } }
TestFan.java
Components of a Class Body.^ Components of a Class Body.
Classes are defined using a class declaration:
[public] class ClassName
{ class-body}
The class body can contain the following elements:^
Variables
^
Methods
^
Constructors: Similar to a method but is run to initialize an objectwhen an instance is created.
−^
must have the same name as the class
−^
it doesn’t have a return type
^
Initializers: Stand-alone blocks of code that are run only once, whenthe class is initialized.
−^
Two types:
Static initializers and
Instance instializers
Other classes: A class can contain other classes (nested classes)
^
Interfaces: Abstract methods and final fields. Will talk about it later.
Constructors^ Constructors
Here’s the basic format for coding a constructor:
public
ClassName
parameter-list
statements...
The body of the constructor should have anything youwant to execute when the object is created in memory.
The most common reason of creating a constructor isto provide initial values for the class variables whenyou create an object.
Constructors^ Constructors
Example: The following is the declaration of the class Actor whichhas a constructor.
public
class
Actor
{
String
lastName; String
firstName; public
Actor(String
last,
String
first)
{
lastName
=^
last;
firstName
=^
first;
} public
void
display()
{ System.out.println(firstName+"
"+lastName);
} }
Constructor: - Same name as the class- No return type
Here is how you create objects usingthe constructor.
Actor
bestActor
=
new
Actor("Smith",
"John");
firstNamelastName
Smith
bestActor
John
firstNamelastName
Allgood
bestSupportingActor
Brad
Actor
bestSupportingActor
=^
new
Actor("Allgood",
"Brad");