Learn the Basics of Classes using Java

A Class is the basic concept in a Object Oriented Programming language. It’s the template for multiple objects with similar features.

Consider the following statements

  1: class Family {
  2: String dadname;
  3: String mumname;
  4: String child;
  5: int dogs; } 

Here, a class called family is created. It is treated as an object. A family may be having dad, mum, children, dogs. These are all its attributes or properties. Now let us examine how the variables plays around the classes.

Normally, we declare variables inside the main statement. But variables declared outside the main method are called as Instance Variables in Java. A special reference (Object) is created inside the main method for the instance variables using dot operator and new keyword. A simple example will help in understanding the concept.

An Illustration

The example given below shows the usage of Instance variables. Variables named x and y are declared above the main method. Inside the main method, object of the class is created and variables are accessed using the dot operator

  1: class Inst {
  2: int x = 30; //instance variable declared
  3: int y = 20; //instance variable declared
  4: Public static void main(String args []) {
  5: Inst in = new Inst(); 
  6: //instance of class using new keyword
  7: System.out.println("The value of x is " +in.x); 
  8: //displays x's value
  9: System.out.println("The value of y is " +in.y); 
 10: //displays y's value
 11: } 
 12: }  

The output of the above code will be:

The value of x is 30

The value of y is 20

Methods in Java

Methods are functions that operate on instances of class. We declare as many functions outside the main method (called Instance Method) and they can be called inside the main method by using dot operator and new keyword (creating object of the class). Let us examine one example:

  1: class Rect {
  2: int length = 30;
  3: int breadth = 30;
  4: void calculate() { //method to calculate area
  5: int area = length * breadth;
  6: System.out.println("The Area is " +area + "sq.cms");
  7: }
  8: public static void main (String args[]) {
  9: Rect p = new Rect();
 10: p.calculate();
 11: }
 12: } 

The output will be

The Area is 900 Sq.cms

In the same way we can pass arguments inside the methods. In the above example instead of declaring variables simply write int <variable name> as a parameter for the calculate function, then you should supply a value as a parameter for the calculate function inside the main function. It’s examined below for your reference:

  1: class Rectparam {
  2: void calculate(int length, int breadth) { //method to calculate area
  3: int area = length * breadth;
  4: System.out.println( " The Area is " +area + "sq.cms");
  5: }
  6: public static void main (String args[]) {
  7: Rect p = new Rect ();
  8: p.calculate(30,30);
  9: }
 10: } 

Java supports method overloading .It means the method name will be same but parameters will be different. Each function takes different parameters like string, integer, float etc. Then an instance of class is created inside the main and appropriate values are passed on to it.

Following example shows the usage of Method Overloading in Java:

  1: class Overload
  2: void show(int x) {
  3: System.out.println("The value is " +x);
  4: }
  5: void show(int a,int b) {
  6: int c = a*b;
  7: System.out.println("The value is " +c);
  8: }
  9: public static void main(String args[]) {
 10: Overload over = new Overload();
 11: over.show(50);
 12: over.show(60,70);
 13: }
 14: } 

Constructors

It is the special kind of method where the constructor name is the same as its class name and it can also be overloaded just like method overloading. The main purpose of a constructor is to provide a way for new objects to be initializes in a special way. Finalizers are used to dispose an object. It is called before Java’s automatic garbage collector reclaims an unused object.

The method to be used is finalize().

Leave a Comment