Wednesday, 15 April 2015

Class in Java

What is a class

In object-oriented terms class provides a blue print for the individual objects created from that class. Once a class is defined it can be used to create objects of that type, thus it can be said that class defines new data type.

Form of a class in Java

A class has certain properties like data (variables) and the code that uses that data in the code logic. Since class provides a template for an object, thus many individual objects of this class type can be created which will have the basic class properties.

A class skeleton

class classname {
 type instance_variable1;
 type instance_variable2;
 …..
 ......

 type methodname1(arguments){
  method code here.....
 }

 type methodname2(arguments){
  method code here.....
 }

}

The variables defined with in a class are called instance variables. The code that operates on these variables is defined with in methods. Together these variables and methods provide the structure of the class.

A simple class

class Rectangle{
    double length;
    double width;
}

/**
 * 
 *
 *
 */
 class RectangleDemo {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.length = 10;
        rectangle.width = 5;
        double area = rectangle.length * rectangle.width;
        System.out.println("Area of the rectangle is " + area);    
    }

}

In this example I have created a class Rectangle which has two instance variables length and width. Then using this template Rectangle (class) new object has been created.

Rectangle rectangle = new Rectangle();

Then values are assigned to the object's instance variables and the area is computed.

We can provide a method in the Rectangle class itself to compute the area, and make the instance variables private to have better encapsulation, the new class will look like this -

class Rectangle{
 private double length;
 private double width;
 // area method to compute area 
 public void area(double ln, double wd){
  length = ln;
  width = wd;
  double area = length * width;
  System.out.println("Area of the rectangle is " + area);
 }
}

/**
 * 
 *
 *
 */
class RectangleDemo {
 public static void main(String[] args) {
  Rectangle rectangle = new Rectangle();
  //rectangle.length = 10;
  //rectangle.width = 5;
  //double area = rectangle.length * rectangle.width;
  //System.out.println("Area of the rectangle is " + area); 
  rectangle.area(5, 10);
 }

}

But you must be wondering why this double work of passing the variables and then in the method assigning the values to the instance variables. Yes we can use constructor to do this. So, we can make the class even better by introducing constructor for the class, the initialization of the instance variables will be done through the constructor in that case. With these changes the class will look like this -

class Rectangle{
 double length;
 double width;
 //Constructor
 public Rectangle(double length, double width) {
  this.length = length;
  this.width = width;
 }
 // Method to compute area
 public void area(){
  double area = length * width;
  System.out.println("Area of the rectangle is " + area);
 }
}

 class RectangleDemo { 
 public static void main(String[] args) {
  Rectangle rectangle = new Rectangle(10, 5);  
  rectangle.area();
 }

}

Modifiers for a class

Modifiers for a class may be classified into two categories -
  • Access modifiers
  • Non-Access modifiers

Access modifiers

Classes can only have the default (package) and public access modifiers assigned to them.
  • Public - class may be declared with the modifier public, in which case that class is visible to all classes everywhere.
  • Default - If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

Note that an inner class can be declared as private but top level class can take only the above mentioned 2 modifiers.

Non Access modifiers

Non access modifiers for the class are -

Points to note -

  • class provides a blue print for the individual objects created from that class.
  • The variables defined with in a class are called instance variables.
  • The code that operates on these variables is defined with in methods.
  • Together these variables and methods provide the structure of the class.
  • Access modifiers used with the class are default (package)and public
  • An inner class can be declared as private.
  • Non access modifiers for the class are final, abstract and strictfp

That's all for this topic Class in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related topics

  1. Object in Java
  2. abstract class in Java
  3. Encapsulation in Java
  4. final in Java
  5. strictfp in Java
  6. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment