OOPJava

30
Classes And Objects Java is strictly an Object-Oriented Programming Language. Java code Class Interface

Transcript of OOPJava

Page 1: 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

Page 2: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 2/30

Classes

Describe the object’s

attributes behaviors

Page 3: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 3/30

Class is a description of anobject.

Page 4: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 4/30

Object is defined to be aninstance of a class

Page 5: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 5/30

An Object consists of 

attributes

behaviors

Page 6: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 6/30

An attribute is a feature of the object,

something the object“has”. 

Page 7: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 7/30

A behavior is something

the object “does” 

Page 8: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 8/30

Each attribute of an objectis denoted as a field in the

class

Page 9: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 9/30

Each behavior of an objectbecomes a method in the

class

Page 10: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 10/30

Procedural ProgrammingFlowcharts

Decomposition

Procedures

Page 11: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 11/30

Object-Oriented ProgrammingClasses

Objects

Inheritance

Encapsulation

Polymorphism

Page 12: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 12/30

Payroll ProgramDetermining objects in problem

• Employees

• Company

• Payroll department• more objects….

Page 13: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 13/30

Employee ClassAttributes

name

address

national id number

Behaviors

hours worked()pay()

Page 14: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 14/30

Writing a Java class

class (keyword)

public class Employee{

}

Page 15: OOPJava

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;}

Page 16: OOPJava

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); }

}

Page 17: OOPJava

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;}

}

Page 18: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 18/30

public class Vehicle

{

int pasg;int fCap;

int mpg;

}

Page 19: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 19/30

Creating Vehicle object

Vehicle jeep = new Vehicle();

Page 20: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 20/30

Dot (.) operator

Object.member

Page 21: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 21/30

Dot operator

 jeep.fCap = 16;

Page 22: OOPJava

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

Page 23: OOPJava

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 ;

Page 24: OOPJava

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.

Page 25: OOPJava

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.

Page 26: OOPJava

8/3/2019 OOPJava

http://slidepdf.com/reader/full/oopjava 26/30

Garbage Collection

Page 27: OOPJava

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()

Page 28: OOPJava

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

Page 29: OOPJava

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;

Page 30: OOPJava

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

}