24
Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo .com

Objects and Classes Mostafa Abdallah [email protected]

Embed Size (px)

Citation preview

Page 1: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Objects and Classes

Mostafa [email protected]

Page 2: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Agenda

• Class and Object.• Defining and using Class.

Page 3: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Class and Object

7-3

Page 4: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Objects in life

• An object represents an entity in the real world that can be distinctly identified.

• For example, a student, a desk, a circle, a button, and car.

7-4

Page 5: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Objects in life

• An object has a state, and behaviors.

• The state of an object consists of a set of data fields (also known as properties) with their current values.

• The behavior of an object is defined by a set of methods.

7-5

Page 6: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

• Class A template or blueprint that describes the kinds of state and behavior that objects of its type support.

• Class Uses: Variables to define data fields. Methods to define behaviors.

Objects and classes

7-6

Page 7: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

• An Object is an instance of a class• That object will have :

Its own state. Access all behaviors defined by its class.

Objects and classes

7-7

Page 8: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Objects and classes

Class Name: Circle

Data field: radius

Methods: getArea()

Circle : Object1

Data field: radius : 10

Circle : Object2

Data field: radius : 25

Circle : Object3

Data field: radius : 125

A Class Template

Three objects of Circle Class

7-8

Page 9: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Defining and using Classes

7-9

Page 10: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Defining Class class Circle {

/** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; }

}

Data field

Method

Constructors

7-10

Page 11: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Data Fields

• A Java class uses variables to define data fields.

• Java assign default values for data fields. – 0 for a numeric type.– False for a boolean type.– '\u0000' for a char type.– Null for reference type.

7-11

Page 12: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Data Fields

• However, Java assigns no default value to a local variable inside a method.public class Student {

String name; // name has default value null

int age; // age has default value 0

}public class Test {

public static void main(String[] args) {

Student student = new Student();

System.out.println("name? " + student.name);

System.out.println("age? " + student.age);

}

}

7-12

Page 13: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Constructors

• A special kind of methods that are invoked to construct objects.

7-13

Page 14: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Constructors, cont

• Must have the same name as the class itself. • Do not have a return type—not even void. • Are invoked using the new operator when an

object is created.• When has no parameters is referred to as a

no-arg constructor.

7-14

Page 15: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Default Constructors

• A class may be declared without constructors. In this case, a no-arg constructor with an empty

body is implicitly declared in the class. • This constructor, called a default constructor.

7-15

Page 16: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Examplepublic class Circle

{

public static final double PI = 3.14159; // A constant public double r;// instance field holds circle’s radius

// The constructor method: initialize the radius field public Circle(double rad) { r = rad; }

// Constructor to use if no arguments public Circle() { r = 1.0; }

// The instance methods: compute values based on radius

public double circumference() { return 2 * PI * r; }

public double area() { return PI * r*r; }

}

7-16

Page 17: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Declaring Object

• To declare a reference variable, use the syntax:

ClassName objectRefVar;

Example:

Circle myCircle;

7-17

Page 18: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Creating Object

• To reference an object, assign the object to a reference variable:

new ClassName();

Example:new Circle();

new Circle(5.0);

7-18

Page 19: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Declaring/Creating Object

ClassName objectRefVar = new ClassName();

Example:Circle myCircle = new Circle();

Create an objectAssign object reference

7-19

Page 20: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Object Types in Memory

1 Primitive type int i = 1 i

Object type Circle c c reference

Created using new Circle()

c: Circle

radius = 1

7-20

Page 21: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Copying Variables

i

Primitive type assignment i = j

Before:

1

j

2

i

After:

2

j

2

c1

Object type assignment c1 = c2

Before:

c2

c1

After:

c2

c1: Circle

radius = 5

C2: Circle

radius = 9

c1: Circle

radius = 5

C2: Circle

radius = 9

7-21

Page 22: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Accessing Objects

• Referencing the object’s data: objectRefVar.data ex: myCircle.radius

• Invoking the object’s method: objectRefVar.methodName(arguments) ex: myCircle.getArea()

Ex: Circle c=new Circle();

c.radius=5;

c.getArea();

7-22

Page 23: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Questions

Page 24: Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

Thanks