Sei sulla pagina 1di 22

Programming with Objects and Classes

OO Programming Concepts Declaring and Creating Objects Constructors Modifiers (public, private)

Object
represents

an entity in the real world that can be distinctly identified. it has a unique identity, state and behaviors.
the state of an object is represented by data fields (also known as properties) The behavior of an object is defined by as set of methods.
is

an instance of a class

Class
is

a template or blueprint that defines what an objects data and methods will be. one can create many instances of a class. Creating an instance is referred to as instantiation. is a construct that defines objects of the same type.

OO Programming Concepts
An object data field 1 ... data field n method 1 ... method n Behavior State
Method findArea A Circle object Data Field radius = 5

Class and Objects


Circle radius findArea new Circle() circle1: Circle radius = 2 ... new Circle() circlen: Circle radius = 5 Graphical notation for objects Graphical notation for classes

Class Declaration
class Circle { double radius = 1.0; double findArea() { return radius*radius*3.14159; }

Declaring Objects
ClassName objectName;

Example:
Circle myCircle;

Creating Objects
objectName = new ClassName();

Example:
myCircle = new Circle();

Declaring/Creating Objects in a Single Step


ClassName objectName = new ClassName();

Example:
Circle myCircle = new Circle();

Differences between variables of primitive Data types and object types


Primitive type Object type int i = 1 Circle c i c 1 reference

c: Circle Created using new Circle(5) radius = 5

Copying Variables of Primitive Data Types and Object Types


Primitive type assignment i=j Object type assignment c1 = c2

Before: i j 1 2

After: i j 2 2

Before: c1 c2 c1: Circle radius = 5

After: c1 c2 c2: Circle radius = 9

Accessing Objects
Referencing

the objects data:

objectName.data
myCircle.radius
Referencing

the objects method:

objectName.method
myCircle.findArea()

Example 1 Using Objects


Objective:

Demonstrate creating objects, accessing data, and using methods. Sample: TestCircle.java

Constructors
is

a special kind of method are invoked when a new object is created. are designed to perform initializing actions such as initializing the data fields of objects. are used to construct objects, has exactly the same name as the defining class. can be overloaded (multiple constructors with the same name but different signatures)

Constructors
Circle(double r) { radius = r; } Circle() { radius = 1.0; } myCircle = new Circle(5.0);

Example 2 Using Constructors


Objective:

Discuss the role of constructors and use them to create objects. Sample: TestCircleWithConstructors.java

TestCircle1.java Circle1.java

Passing Objects to Methods


Passing Passing

by reference by value

Example : Passing Objects as Arguments TestPassingObject.java

Visibility Modifiers and Accessor Methods


By default, the class, variable, or data can be accessed by any class in the same package.

public
The class, data, or method is visible to any class in any package.

private

The data or methods can be accessed only by the declaring class. The getter and setter accessor methods are used to read and modify private properties.

Example 4 Using the private Modifier and Accessor Methods


In this example, private data are used for the radius and the accessor methods getRadius and setRadius are provided for the clients to retrieve and modify the radius.

TestCircleWithPrivateModifier.java

Exercise:
Write a class named Rectangle to represent rectangles. The data fields are height and width declared as private. You need to provide the assessor methods for the properties and a method findArea() for computing the area of the rectangle. Write a client program to test the class Rectangle. In the client program, create two Rectangle objects. Assign any widths and heights to the two objects. Display

both object's properties and find their areas. Call your setter method that would update the width and height of the rectangle by 20%.

Sample Output:

Potrebbero piacerti anche