Classes in Java

Class

In Object Oriented programming, “class is a blue print that is used to create objects”
A class is a collection of things that share common attributes like class of students.

Some characteristic of a class

  • A class defines a new data type
  • The Variables that are defined inside a class are called instance variables because each instance/object of the class contains its copy of these variables.
  • Thus, the data for one object is separate and unique from the data for another.

Structure of a class

Here is a structure of a class

class ClassName { type instanceVariable1; type instanceVariable2; //... type instanceVariableN; type methodName1(parameterList) //body of method } type methodName2(parameterList) { //body of method } //... type methodNameN(parameterList) { //body of method } } class Container double breadth; double height; double depth; }

Let us explain the above class, it contains three instance variables
Breadth, height and depth but it does not contain any methods.

Now question arises

  • How can we obtain methods of a class?

We can obtain objects of a class by the following two methods:

  1. Declare a variable of the class type
  2. Declare an actual, physical object and assign it to that variable

Now, we will create an object of container class by using the following technique

Box mycontainer = new Container(); // creates an object of container class

  • Now what is new operator?

    • New
      • The new operator dynamically allocates memory for an object and returns a reference to it

Object

Definition
Objects are considered key to object oriented programming

“An object is an instance of class”

Real world object has the two common characteristics

  • State
  • Behavior

In object oriented programming, object contains methods and attributes. So a software object has methods (behavior) and attributes (state) and occupies space in the memory. In fact, class does not occupy any space in the memory its only a blue print.

Characteristics of objects

  • Objects have physical reality
  • Every Containerobject will contain its own copies of the instance variables breadth, height, and depth
  • To access these variables, we will use the dot (.) operator
  • The dot operator links the name of the object with the name of an instance variable or a method

Example:

- mycontainer.breadth=100;

Constructors

A constructor initializes an object immediately upon creation.

Constructor has same name as the class in which it resides.

Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.

Constructors have no return type, not even void.

This is because the implicit return type of a class constructor is the class type itself.

class Container double breadth; double height; double depth; // constructor for Container. Container() { System.out.println("Constructing Container"); breadth = 100; height = 100; depth = 100; } // compute and return volume double volume() { return breadth * height * depth; } }

When we do not define a constructor then Java creates a default constructor for the class.

The default constructor automatically initializes all instance variables to zero.

Once we define our own constructor, the default constructor is no longer used.

Garbage Collection

How the objects are destroyed?

How memory is released for later reallocation?

Java handles de-allocation automatically.

The technique that accomplishes this is called garbage collection.

It works like this:

  • When no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
  • Garbage collection only occurs occasionally during the execution of the program.
  • It will not occur simply because one or more objects exist that are no longer used.

Add new comment