Upload
nora-lucas
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Objects and Classes
Mostafa [email protected]
Agenda
• Class and Object.• Defining and using Class.
Class and Object
7-3
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
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
• 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
• 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
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
Defining and using Classes
7-9
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
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
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
Constructors
• A special kind of methods that are invoked to construct objects.
7-13
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
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
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
Declaring Object
• To declare a reference variable, use the syntax:
ClassName objectRefVar;
Example:
Circle myCircle;
7-17
Creating Object
• To reference an object, assign the object to a reference variable:
new ClassName();
Example:new Circle();
new Circle(5.0);
7-18
Declaring/Creating Object
ClassName objectRefVar = new ClassName();
Example:Circle myCircle = new Circle();
Create an objectAssign object reference
7-19
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
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
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
Questions
Thanks