Skip to main content

Java Classes, Object and Methods, Constructors

 Defining a class

    A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create "Variables" which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows:

class classname [extends superclassname]{

    [fields declaration]

    [method declaration]

}

The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example,

class Empty{

}

In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything.

A java class content:

  •     Field or Fields declaration
  •     Methods declaration
  •     Constructor
  •     Block

Field OR Fields declaration:

    Data is encapsulated in a class by placing fields inside the body of class defination. These variables called instance variables because they are created whenever object of the class instantiated. We declare instance variable exactly same way we declare local variables. For example,

class Rectangle{

    int length;

    int width;

}

The class Rectangle have two integer type instance variables. It also allows to declare them in line like,

    int length, width;

    Remember these variables are only declare and therefor no storage space has been created in memory for them.

Methods declaration:

    A class with only data fields has no life. The objects created by such a class cannot respond any message. We must add method which is nessasary to manipulate the data contained in class. Methods are declare insode body of the class but immediately after the declareation of instance variables. The general form of method declaration is as follows:

type methodname(parameter list){

    method-body;

}

Method has four basic parts:

  • The name of method (methodname)
  • Type of the value method returns (type).
  • A list of parameters (parameter list)
  • The body of method.
For example,
class Rectangle{
    int width;
    int length;
    void getData(int x, int y){    //method declaration
        length=x;
        width=y;
    }
}

There are two types of methods in java Predefine and User-define method.
The methods which are already define into java class liberary is called Predefine method like length(), equals(), comparTo() etc.
The methods which are written by user or programmer is called User-define method. 

Constructors:
    A constructors are as good as methods. Constructors have same name as class name. They do not have any return type. They called implecitly at the time of creation of object with new keyword. They are normaly use to assign value to the set of instance variables. It can not be call explicitly. There are two types of constructors:
  1. Default Constructors.
  2. Parameterised constructors.

1. Default Constructors:
    A constructor does not have any parameter called default constructors. It is automatically called when we create the object with new keyword. For example,
class Bike{
    Bike(){
        System.out.println("Bike constructor is created.");
    }
    public static void main(String args[]){
        Bike bike=new Bike();
    }
}
Output:
Bike constructor is created.

2. Parameterised Constructors:
    A constructor with specific number of parameter called parameterised constructors. It is use to provide different values to different objects. For example,
class Student4{
    int id;
    String name;
    
    Student4(int i,String n){
    id = i;
    name = n;
    }
    void display(){System.out.println(id+" "+name);}
 
    public static void main(String args[]){
    Student4 s1 = new Student4(111,"Karan");
    Student4 s2 = new Student4(222,"Aryan");
    s1.display();
    s2.display();
   }
}
Output:
111 Karan
222 Aryan



Comments