Classes & Objects
Classes & Objects
Syllabi
• Introducing Classes: Class Fundamentals,
Declaring Objects, Assuming Object reference
Variables, Introducing Methods, Constructors, The
this Keyword, Garbage Collection, The Finalize()
method, A Stack class.
Class Fundamentals
• A class, defines a new data type.
• Once defined, this new type can be used to
create objects of that type.
• Thus, a class is a template for an object, and an
object is an instance of a class.
• Because an object is an instance of a class, the
two words object and instance used
interchangeably.
The General Form of a Class
• A class is declared by use of the class keyword.
• Classes may contain only code or only data, most real-world classes contain
both.
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
The General Form of a Class
• The data, or variables, defined within a class are
called instance variables.
• The code is contained within methods.
• Collectively, the methods and variables defined
within a class are called members of the class.
• Variables defined within a class are called
instance variables because each instance of the
class (that is, each object of the class) contains its
own copy of these variables. Thus, the data for
one object is separate and unique from the data
for another.
A Simple Class
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
double volume() {
return width * height * depth;
}
Adding a Method That Takes Parameters
}
void pop()
{
if(top==-1)
{
System.out.println(“Stack Underflow”);
}
else
{
System.out.println(“Poped Element is :” +a[top--]);
}
}
The this Keyword
• Sometimes a method will need to refer to the
object that invoked it.
• To allow this, Java defines the this keyword.
• this can be used inside any method to refer to the
current object. That is, this is always a reference to
the object on which the method was invoked.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}
• Usage of java this keyword
1. this can be used to refer current class instance variable.
2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the
method.
Instance Variable Hiding
• It is illegal in Java to declare two local variables with the same name
inside the same or enclosing scopes.
• Local variables, including formal parameters to methods, which overlap
with the names of the class’ instance variables.
• However, when a local variable has the same name as an instance
variable, the local variable hides the instance variable.
• this, resolve any name space collisions that might occur between
instance variables and local variables.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
Overloading constructors
public class Rectangle {
int x, y;
int width, height;
Rectangle()
{
this(0, 0, 1, 1);
}
Rectangle(int width, int height)
{
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public class student
{
private int rollNum;
student()
{
rollNum =100;
}
student(int rnum)
{
this();
rollNum = rollNum+ rnum;
}
public int getRollNum() {
return rollNum;
}
public void setRollNum(int rollNum) {
this.rollNum = rollNum;
}
}
Garbage Collection
• Since objects are dynamically allocated by using the new operator, how
such objects are destroyed and their memory released for later
reallocation?
• In some languages, such as C++, dynamically allocated objects must
be manually released by use of a delete operator.
• Java takes a different approach; it handles deallocation automatically.
The technique that accomplishes this is called garbage collection.
• When no references to an object exist, that object is assumed to be
unreachable, and the memory occupied by the object can be
reclaimed.
• Advantage of Garbage Collection
• o It makes java memory efficient because garbage collector removes the unreferenced objects from heap
memory.
• o It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
Ways to collect Garbage
• By anonymous object: new Student();
• By nulling reference:
• Student s = new Student();
• s = null;
• By assigning a reference to another object:
• Student s1 = new Student();
• Student s2 = new Student();
• s1 = s2;
• The garbage collector in Java can be called explicitly
using the following method:
• System.gc();
static {
//code
}
class UseStatic
{
static int a = 3;
static int b;
static void display (int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static
{
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[])
{
display (42);
}
}
Calling static outside the class
• With class name
Recursion
• recursion is the attribute that allows a method to call itself.
Access Control
• The access modifiers in java specifies accessibility (scope) of a data
member, method, constructor or class.
• There are 4 types of java access modifiers:
1. private
2. default
3. protected
4. public
Public
private:
protected:
default
Object as Parameters
class Add public class classExAdd
{ {
int a; public static void main(String
int b; arg[])
{
Add(int x,int y)// parametrized constructor Add A=new Add(5,8);
{ A.sum(A);
a=x; }
b=y; }
}
void sum(Add A1) {
int sum1=A1.a+A1.b;
System.out.println("Sum of a and b
:"+sum1);
}
}
• Write a java program to create 2 objects of complex numbers and
pass these objects as parameters to the methods. Perform addition of
2 complex numbers and return the sum as an object.
final