Lecture 9 Class and Objects
Lecture 9 Class and Objects
Lecture 9 Class and Objects
Technology
CIC-212
Java Programming
Lecture
Classes and Objects
Classes and Objects
A class consists of variables
called fields together with
functions called methods or
member functions that act on
those fields.
General Structure of a Class
Members of a class are:
• fields or variables
– a class or object's state or attributes
• methods
– a class or object's behaviour
• constructors
– to make objects
• each has access control
General Structure of a Class
Types of Members
• fields or variables
– instance unique values for each object
– class uses the static modifier
same value for all objects
• methods
– instance work with both instance
and static variables
– class uses static modifier
works with static variables
General Structure of a Class
Fields or Variables
field or variable holds a single value
is strongly typed: must be declared
type specifies the kind of value and the variable's
behaviour
int i = 123; // integer primitive
Point pt = null; // object reference
pt = new Point (x, y); // coordinates
pt has the value of the Point object's address, not
the x, y coordinates
Classes and Objects
A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
Circle
centre
radius
circumference()
area()
Classes and Objects
• A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
• The basic syntax for a class definition:
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
• Bare bone class – no fields, no methods
• Add fields
return 3.14 * r * r;
}
}
Classes and Objects
Data Abstraction
• Declare the Circle class, have created a new
data type – Data Abstraction
Circle aCircle;
Circle bCircle;
Classes and Objects
Class of Circle cont.
• aCircle, bCircle simply refers to a Circle
object, not an object itself.
aCircle bCircle
null null
bCircle = aCircle;
Classes and Objects
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P Q P Q
Classes and Objects
Automatic garbage collection
• The object
Q
does not have a reference and
cannot be used in future.
double area;
aCircle.r = 1.0;
area = aCircle.area();
Classes and Objects
Using Circle Class
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
[raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
Constructors
• a special kind of method to construct an instance
of an object from a class template
• default constructor is ClassName()
– created if the class has no constructors
• instance fields are initialized to default values
automatically (see next slide)
• overloaded constructors: same name with
different parameter lists
• use of “this” to call another constructor
Constructors
Java automatically initializes class (static) and object
(instance) fields
MyClass(int maximum) {
this.maximum = maximum;
}
Syntax
final type constName = value;
Example
final int FILE_NEW = 1;
final float PI = 3.141519;
Final Variables
Subsequent parts of the program may use the above
final variables FILE_NEW and PI
class A { class B {
final int X = 10; final int X = 10;
public static void main (String args[]) public static void main(String args[]) {
{ X = 20;
System.out.println(“ X is “+X); System .out. println(“ X is “+X);
} }
} }
final variable
should not change
Other Uses Of final