You are on page 1of 27

Classes and Objects

Class Fundamentals – Access and non-access specifiers – Declaring


objects and assigning object reference variables – Array of objects –
constructors and destructors – usage of this and static keywords
Class
Fundamentals
• Components of a class
• Attributes
• Methods
Declaring objects and assigning object
reference variables
<Class name> <identifier>; //Declating reference for the class
<identifier> = new <Class name>(); //Creating object for the class and
assigning the address of the object created to the reference
Example:
Circle c1;
c1 = new Circle();
Attributes and methods can be access using the reference name and a
dot operator.
//Defining Circle class

Creating
references c1,
c2 pointing to
objects
Specifiers for class, attributes, methods, local
variables
• Control accessibility and visibility (access specifiers)
• Other functionalities and feature (non-access
specifiers)
Inheritance

Multithreading

Serialisation Consistent floating point operations across


platforms
Access and Non access specifiers for a class
• Access specifiers
• public
• private
• protected
• default
• Non access specifiers
• static
• final
Access specifiers – for methods and variables
in a class
Access specifier Description

private Access the variables and methods by the instructions within


class

protected Access the variables and methods within and within the
sub-classes across packages
Decreasing
default / no access specifier Access the variables and methods within and outside the restrictiveness
class within package

public Access the variables and methods within and outside the
class across packages
c2
Implementing Encapsulation using private and public / default
Array of objects

• Declaring the array of references


<Class_name> <identifier>[]; (or)
<Class_name>[] <identifier>;
Circle[] collectionOfCircles; or Circle collectionOfCircles[];
• Allocating space for the references
<identifier>[<index>] = new <Class_name>[number of references]
collectionOfCircles = new CollectionOfCircles[50];
• Accessing references and creating objects
<identifier>[<index>] = new <identifier>();
collectionOfCircles[0] = new Circle(); //can also be done using for loop for all the references
• Accessing object attributes and methods using the array of references
collectionOfCircles[0].setX(1); collectionOfCircles[0].setY(0);
collectionOfCircles[0].radius(1);
collectionOfCircles.area();
Creating array of circles
Constructors
• Used for assigning values to the attributes during the creation of
object
• Methods in a class with no return type with the name same as the
class name
• Types
• Default
• Parameterised
• Copy
Circle with constructors
Circles using constructors
Destructors
• Destructor in C, C++
• Used to release the memory and resources occupied by the objects
• The functionality of Destructor in Java
• Performed by the Garbage collector (program that runs in the JVM)
• Method – finalize works same as destructor
More on finalize
• It is not a destructor but it provides extra security.
• It ensures the use of external resources like closing the file, etc. before
shutting down the program. We can call it by using the method itself
or invoking the method System.runFinalizersOnExit(true).
• It is a protected method of the Object class that is defined in
the java.lang package.
• It can be called only once.
• We need to call the finalize() method explicitly if we want to
override the method.
• The gc() is a method of JVM executed by the Garbage
Collector. It invokes when the heap memory is full and
requires more memory for new arriving objects.
• Except for the unchecked exceptions, the JVM ignores all the
exceptions that occur by the finalize() method.
“this” keyword
• this – indicates the invoking object
• Commonly used to differentiate formal parameters and the attributes of the
class
“static” keyword
• Member that can be used by itself, without reference to a specific
instance. To create such a member, precede its declaration with the
keyword static. When a member is declared static, it can be accessed
before any objects of its class are created, and without reference to any
object.
• You can declare both methods and variables to be static. The most
common example of a static member is main( ). main( ) is declared as
static because it must be called before any objects exist.
• Static variables
• Instance variables declared as static are, essentially, global variables. When objects of its class
are declared, no copy of a static variable is made. Instead, all instances of the class share the
same static variable.
Example static variable – counting objects
Example – static variable – as global variable
A C Program with global variable –
counting function calls
Equivalent Java Program
Static methods
• Methods declared as static have several restrictions:
• They can only call other static methods.
• They must only access static data.
• They cannot refer to this or super in any way. (The keyword super relates to inheritance and
is described in the next chapter.)

You might also like