OOPJava
-
Upload
anandakarunatillaka -
Category
Documents
-
view
217 -
download
0
Transcript of OOPJava
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 1/30
Classes And Objects
Java is strictly an Object-Oriented
Programming Language.
Java code Class
Interface
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 2/30
Classes
Describe the object’s
attributes behaviors
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 3/30
Class is a description of anobject.
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 4/30
Object is defined to be aninstance of a class
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 5/30
An Object consists of
attributes
behaviors
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 6/30
An attribute is a feature of the object,
something the object“has”.
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 7/30
A behavior is something
the object “does”
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 8/30
Each attribute of an objectis denoted as a field in the
class
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 9/30
Each behavior of an objectbecomes a method in the
class
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 10/30
Procedural ProgrammingFlowcharts
Decomposition
Procedures
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 11/30
Object-Oriented ProgrammingClasses
Objects
Inheritance
Encapsulation
Polymorphism
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 12/30
Payroll ProgramDetermining objects in problem
• Employees
• Company
• Payroll department• more objects….
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 13/30
Employee ClassAttributes
name
address
national id number
Behaviors
hours worked()pay()
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 14/30
Writing a Java class
class (keyword)
public class Employee{
}
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 15/30
Employee class
public class Employee
{
public String name;public String address;
public String id;
public double hPay;}
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 16/30
public class Employee
{
public String name;public String address;
public String id;
public double hPay;
public void writeDetails
{
System.out.println(name + “” + id); }
}
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 17/30
public class Employee
{
public String name;public String address;
public String id;
public double hPay;
public double computePay
{
return hPay * 40;}
}
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 18/30
public class Vehicle
{
int pasg;int fCap;
int mpg;
}
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 19/30
Creating Vehicle object
Vehicle jeep = new Vehicle();
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 20/30
Dot (.) operator
Object.member
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 21/30
Dot operator
jeep.fCap = 16;
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 22/30
Instantiating an Object
Vehicle jeep ;
jeep = new Vehicle() ;
Create an object in memory
and returns a reference
reference jeep is pointing to
the Vehicle object in
memory
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 23/30
jeep.psg = 8 ;
jeep.fCap = 24 ;
Jeep.mpg = 44 ;
car.psg = 4 ;
car.fCap = 18 ;
car.mpg = 28 ;
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 24/30
Constructor
Initializes an object when its created.
Has the same name as its class.
Syntactically similar to a method.
Has no explicit return type.
Give initial values to the instance variables.
Perform startup procedures.
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 25/30
Constructor
All classes have constructors.
Java automatically defines default constructor
that initializes all member variables to zero.
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 26/30
Garbage Collection
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 27/30
Vehicle jeep = new Vehicle()
Vehicle jeep = new Vehicle()
Vehicle jeep = new Vehicle()
Vehicle jeep = new Vehicle()
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 28/30
Vehicle j1 = new
Vehicle(); j1
Vehicle j2 = new
Vehicle();
j2Vehicle j3 = j2;
j3
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 29/30
Vehicle j1 = new
Vehicle(); j1
Vehicle j2 = new
Vehicle();
j2Vehicle j3 = j2;
j3 j1 = j2;
8/3/2019 OOPJava
http://slidepdf.com/reader/full/oopjava 30/30
The finalize() Method
Called just before an object’s final destruction by
the garbage collector.
protected void finalize()
{
// code
}