Objects and Classes

26
Objects and Classes Creating Objects and Object Reference Variables Differences between primitive data type and object type Constructors Modifiers (public, private and static) Instance and Class Variables and Methods Scope of Variables Use the this this Keyword

description

Objects and Classes. Creating Objects and Object Reference Variables Differences between primitive data type and object type Constructors Modifiers ( public , private and static ) Instance and Class Variables and Methods Scope of Variables Use the this Keyword. Class Concept. - PowerPoint PPT Presentation

Transcript of Objects and Classes

Page 1: Objects and Classes

Objects and Classes Creating Objects and Object Reference Variables

– Differences between primitive data type and object type Constructors Modifiers (public, private and static) Instance and Class Variables and Methods Scope of Variables Use the thisthis Keyword

Page 2: Objects and Classes

Class Concept

data field 1

method n

data field n

method 1

An object

...

...

State

Behavior

Data Fieldx0=1.0y0=2.0

radius = 5

MethodFindArea()

A Circle object

Page 3: Objects and Classes

UML: Unified Modeling Language

Circle_1: Circle

x0 = 1.0y0 = 2.0

radius = 2

new Circle()

Circle_n: Circle

x0 = 3.0y0 = 10.0radius = 5

new Circle()

...

UML Graphical notation for classes

UML Graphical notationfor objects

Circle

x0: doubley0: doubleradius: double

findArea(): double

UML Graphical notation for fields

UML Graphical notation for methods

Page 4: Objects and Classes

Class Circle Declarationclass Circle { double x0 = 0.0; double y0 = 0.0; double radius = 1.0;

double findArea() { return radius*radius*3.14159; }}

Page 5: Objects and Classes

Declaring Object Reference Variables

ClassName objectName;

Example:Circle myCircle;

Page 6: Objects and Classes

Creating ObjectsobjectName = new ClassName();

Example:myCircle = new Circle();

The object reference is assigned to the object reference variable.

Page 7: Objects and Classes

Declaring/Creating Objectsin a Single Step

ClassName objectName = new ClassName();

Example:Circle myCircle = new Circle();

Page 8: Objects and Classes

Test Class Circlepublic class TestCircle { public static main(String args[]) { double area1, area2; Circle myCircle1 = new Circle(); Circle myCircle2 = new Circle();

area1 = MyCircle1.findArea(); area2 = MyCircle2.findArea(); System.out.println (”S1=”+area1); System.out.println (”S2=”+area2); }}

Page 9: Objects and Classes

Differences between variables of primitive Data types and Object types

1

c: Circle

radius = 1

Primitive type int i = 1 i

Object type Circle c c reference

Created using new Circle()

Page 10: Objects and Classes

Object reference The object reference is a handle to the location where the object resides in memory.

When a primitive value is passed into a method, a copy of the primitive is made. The copy is what is actually manipulated in the method. So, the value of the copy can be changed within the method, but the original value remains unchanged.

When an object reference or array reference is passed into a method, a copy of the reference is actually manipulated by the method. So, the method can change the attributes of the object.

Page 11: Objects and Classes

Accessing Objects Referencing the object’s data: objectName.data Example: r= myCircle.radius; myCircle.radius=10.0;

Invoking the object’s method: objectName.method() Example: S = myCircle.findArea();

Page 12: Objects and Classes

ConstructorsCircle() { x0 = 0.0; y0 = 0.0; radius = 1.0; }

Circle(double x, double y, double r) { x0 = x; y0 = y; radius = r; }

Constructors are a special kind of methods that are invoked to construct objects.

Page 13: Objects and Classes

Constructors, cont.A constructor with no parameters is referred to as a default constructor.

·       Constructors must have the same name as the class itself.

·       Constructors do not have a return type—not even void.

·       Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

Page 14: Objects and Classes

Constructors and method for Class Circle

public class Circle { ... Circle() // Default constructor { x0 = 0.0; y0 = 0.0; radius = 1.0; } Circle(double x, double y, double r) { x0 = x; y0 = y; radius = r; } findArea() { return radius*radius*3.14159; }}

Page 15: Objects and Classes

Default Constructor If a class does not define any constructor explicitly, a default constructor is defined implicitly. If a class defines constructor implicitly, a default

constructor does not exist unless it is defined explicitly.

Page 16: Objects and Classes

Using Constructors Objective: Demonstrate the role of

constructors and use them to create objects.

myCircle = new Circle();myCircle = new Circle(2,.0,3.0,5.0);

Page 17: Objects and Classes

Visibility Modifiers and Accessor Methods

By default, the class, variable, or data can beaccessed by any class in the same package (will be explained later ). 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.

Page 18: Objects and Classes

Visibility modifiers and accessor methodspublic class Circle { private double x0 = 0.0; private double y0 = 0.0; private double radius = 1.0;

public double getRadius() { return radius; } public void setRadius(double newRadius) { radius = newRadius; } public void setRadius(int newRadius) { radius = newRadius; } …}

Page 19: Objects and Classes

Instance Variables and Methods

Instance variables belong to a specific instance.

Instance methods are invoked by an instance of the class.

Page 20: Objects and Classes

Class Variables, Constants, and Methods

Class variables are shared by all the instances of the class.

Class methods are not tied to a specific object.

Class constants are final variables shared by all the instances of the class.

Page 21: Objects and Classes

Class Variables, Constants, and Methods, cont.

To declare class variables, constants, and methods, use the staticstatic modifier.

private static int numberOfObjects=0;

public final static double PI = 3.1415926;

Page 22: Objects and Classes

Class Variables, Constants, and Methods, example.

public class Circle { private static int numberOfObjects=0; public final static double PI = 3.1415926;

Circle(double x, double y, double r) { x0=x; y0=y; radius=r; numberOfObjects++; } public static getNumberOfObjects() { return numberOfObjects; } …}

Page 23: Objects and Classes

Class Variables, Constants, and Methods, cont.

CircleWithStaticVariable -radius -numOfObjects +getRadius(): double +setRadius(radius: double): void +getNumOfObjects(): int +findArea(): double

1 radius circle1:Circle -radius = 1 -numOfObjects = 2

instantiate

instantiate

Memory

2

5 radius

numOfObjects

radius is an instance variable, and numOfObjects is a class variable

UML Notation: +: public variables or methods -: private variables or methods underline: static variables or metods

circle2:Circle -radius = 5 -numOfObjects = 2

Page 24: Objects and Classes

Math is class with private constructor

Class Math contains only public static methods:

Math.sin() Math.asin () Math.ceil() Math.round() Math.abs() Math.min() Math.pow() Math.exp() Math.sqrt() ...

Page 25: Objects and Classes

Scope of Variables

The scope of instance and class variables is the entire class. They can be declared anywhere inside a class.

The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.

Page 26: Objects and Classes

The Keyword this Use this to refer to the current object. Use this to invoke other constructors of the object.

public void setRadius(double radius) { this.radius = radius; }

public Circle() { this(0.0, 0.0, 1.0); }