Classes And Objects

A class is a model or blueprint for creating objects which means that the properties (variables) and actions (methods) of the objects are written in the class. The same variables and methods are available in the objects because they are created from the class

Creating Classes And Objects

Observe the following code which shows the creation of class and objects, calling the method etc.

// creating class and object
class Person {

    // properties (variables)
    String name;
    int age;

    // actions (methods)
    void talk() {
        System.out.println("Hello I am " + name);
        System.out.println("My age is " + age);
    }
}

// Another Example
class Demo {
    public static void main(String args[]) {
// create person class object: Anu
        Person anu = new Person();

// calling the talk method
        anu.talk();

    }
} 

Here the output will be -

Hello I am null
My age is 0

In the above program we created the object Anu but we did not initialize the instance variable. Initialization means storing the starting data, since we did not initialize the variable, java compiler adds no additional code to person class.

Initialization Of Instance Variables

class Demo {
    public static void main(String args[]) {
// create person class object: Anu
        Person anu = new Person();

// initialize the instance variables using reference
        anu.name = "Anu";
        anu.age = 25;

// calling the talk method
        anu.talk();

    }
}

Here the output will be

Hello I am Anu
My age is 25

In this way we can initialize the instance variables of person class in some other class i.e. Demo class. This violates the security of the person class. For this purpose, we can use private access specifier while declaring the variables in person class.

Access Specifiers

Private

Private members of the class are not accessible any where outside the class. They are accessible only within the class by methods of that class.

Public

Public members of the class are accessible every where outside the class.

Protected

Protected members of a class are accessible outside the class, but generally within the same directory.

Default

Default members are accessible outside the class, but within the same directory.

Constructors

A constructor is similar to a method that it is used to initialize the instance variables. The characteristics of constructor are as follows

  • The constructor name and the class name should be same
  • Constructor name should end with a pair of simple braces
  • Constructor don’t return any value, not even void
  • Constructor is automatically called and created at the time of creating an object
  • Constructor is called and executed only once per object

Initializing the instance variable using the default and parameterized constructor is shown below:

//initializing the instance variable using constructor
// creating class and object
class Person {

    // instance variables
    private String name;
    private int age;

    // default constructor
    Person() {
        name = "Anu";
        age = 25;
    }

    // parameterized constructor
    Person(String s, int i) {
        name = s;
        age = i;

    }

    // actions (methods)
    void talk() {
        System.out.println("Hello I am " + name);
        System.out.println("My age is " + age);
    }
}

class Demo {
    public static void main(String args[]) {
// create person class object: Anu, here default constructor is called
        Person anu = new Person();

// calling the talk method
        anu.talk();

// create Rohithobject, here parameterized constructor is called
        Person rohith = new Person("Rohith", 26);

// calling the talk method
        rohith.talk();
    }
}

Here the output will be -

Hello I am Anu
My age is 25
Hello I am Rohith
My age is 26

Subscribe For More Content