Chapter 3
Chapter 3
Concept of encapsulation
• The concept of class is the logical construct upon which the entire Java
language is built.
What is a class?
• A class is a group of objects, which have common
properties. Fields
• It is a template or blueprint from which objects are
created. Methods
• It is a logical entity.
Constructors
A class in Java can contain:
Blocks
• Fields
• Methods Nested Class
• Constructors
• Blocks Interfaces
• Nested class(es) and interface(s)
General structure of a class
class <class-name>{
<type> <variable 1>;
<type> <variable 2>;
<type> <variable 3>;
...
<type> <variable n>;
class Circle {
double x, y; // The coordinates of the center r
double r; // The radius
} (x, y)
Adding methods to Circle class
class Circle {
double x,y; // The coordinates of the center
double r; // The radius
class Box{
class Circle {
double width;
double x,y; double height;
double r; double depth;
double circumference(){ double area(){
double a;
return 2*3.14159*r;
a = (width*height + height*depth + width*depth) * 2;
} return a;
double area(){ }
return (22/7)*r*r; double volume(){
double v;
}
v = width*height*depth;
} return v;
}
}
Multiple class objects
3. The name of the program file should be same as the name of the main
class followed by .java as an extension.
}
return (22/7)*r*r; Name the file as Test.java.
}
class Box{
This program reports compilation error as follows.
double width;
double height;
double depth;
double area(){
double a;
a = (width*height + height*depth + width*depth) * 2;
return a;
}
double volume(){
double v;
v = width*height*depth;
return v;
}
}
Method with parameters
class Circle { class CircleDemo3 {
double x,y; public static void main(String args[]){
double r; Circle c1 = new Circle();
double circumference(){ Circle c2 = new Circle();
return 2*3.14159*r; // Initialize the circles
} c1.setCircle(3.0,4.0,5.0);
double area(){ c2.setCircle(-4.0,8.0,10.0);
return (22/7)*r*r; System.out.println("Circumference Circle 1" + c1.circumference());
} System.out.println("Area of circle 1" + c1.area());
void setCircle(double a, double b, double c){ System.out.println("Circumference Circle 2" + c2.circumference());
x = a; // Set center x-coordinate System.out.println("Area of circle 2" + c2.area());
y = b; // Set center y-coordinate }
r = c; // Set radius }
}
}
3. This method has the same name as the class in which it resides.
4. Once defined, the constructor is automatically called immediately after object is created.
6. In fact, the implicit return type of a class constructor is the class type itself.
2. Sometimes a method will need to refer to the object that invoked it.
4. this can be used inside any method to refer to the current object.
5. That is, this is always a reference to the object on which the method
is invoked.
Constructor : An example
class Circle { class Circle {
double x,y; double x,y;
double r; double r;
double circumference(){ double circumference(){
return 2*3.14159*r; return 2*3.14159*r;
} }
double area(){ double area(){
return (22/7)*r*r; return (22/7)*r*r;
} }
void setCircle(double a, double b, double c){ Circle (double x, double y, double r){
x = a; // Set center x-coordinate this.x = x; // Set center x-coordinate
y = b; // Set center y-coordinate this.y = y; // Set center y-coordinate
r = c; // Set radius this.r = r; // Set radius
} }
} }
class CircleDemo5 {
public static void main(String args[]){
Circle c1 = new Circle();
c1.setCircle(3.0,4.0,5.0);
Circle c2 = new Circle (-4.0,8.0,10.0);
System.out.println("Circumference Circle 1" + c1.circumference());
System.out.println("Area Circle 1" + c1.area());
System.out.println("Circumference Circle 2" + c2.circumference());
System.out.println("Area Circle 2" + c2.area())
}
}
1. There is a specialized use of this keyword that arises when a class has
multiple constructors.
2. In that case, this can be used from one constructor to invoke one of the
other constructor of the same class.
this with multiple constructors : An example
class Circle {
double x, y;
double r;
Circle (double x, double y, double r){
this.x = x; this.y = y; this.r = r;
}
Circle (double r){
this(0.0, 0.0, r);
} Note:
Circle (Circle c){
1. There is a very important restriction on the this syntax.
this(c.x, c.y. c.r);
} 2. It should appear only as the first statement in a
Circle (){ constructor.
this(0.0, 0.0, 1.0);
}
double circumference(){
return 2*3.14159*r;
}
double area(){
return (22/7)*r*r;
}
}
Questions to think…