Class, Object and Methods

Class : Whatever we can see in this world all the things are a object. And all the objects are categorized in a special group.

Class, Object and Methods

Class, Object and Methods

     

Class : Whatever we can see in this world all the things are a object. And all the objects are categorized in a special group. That group is termed as a class. All the objects are direct interacted with its class that mean almost all the properties of the object should be matched with it's own class. Object is the feature of a class which is used for the working on the particular properties of the class or its group. We can understand about the class and object like this : we are all the body are different - different objects of the human being class. That means all the properties of a proper person relates to the human being. Class has many other features like creation and implementation of the object, Inheritance etc.

In this Program you will see how to use the class, object and it's methods. This program uses the several values of several defined variables for getting the Area and Perimeter of the Square and Rectangle  by calling the different functions from the different classes through the several objects created for the several class. In this program there are two classes has been used except the main class in which the main function is declared. First class is square which is used for getting the Area and Perimeter of the square and another class is rectangle which is used for getting the Area and Perimeter of the Rectangle. All the functions in the square and rectangle class are calling with different - different arguments two times for getting the Area and Perimeter of square and rectangle for two different sides. This program gives us the Area and Perimeter for the different sided Square and Rectangle separately. Full running code is given with the example : 

Here is the code of the program : 

class square{
  int sqarea(int side){
  int area = side * side;
  return(area);
  }
  int sqpari(int side){
  
int pari = 4 * side;
  return(pari);
  }
}

class rectangle{
  int rectarea(int length,int breadth){
 
 int area = length * breadth;
  return(area);
  }
  int rectpari(int length,int breadth){
  int pari = 2*(length + breadth);
  return(pari);
  }
}
public class ObjectClass{
  
public static void main(String args[]){
  int sarea1,sarea2,pari1,pari2;
  int rectarea1,rectarea2,rectpari1,rectpari2;
  square sq =
 new square();
  rectangle rect = new rectangle();
 
 int a=20;
  System.out.println(
"Side of first square = " + a);
  sarea1 = sq.sqarea(a);
  pari1 = sq.sqpari(a);
  System.out.println("Area of first square = " + sarea1);
  System.out.println("Parimeter of first square = " + pari1);
  a =
 30;
    System.out.println("Side of second square = " + a);
  sarea2 = sq.sqarea(a);
  pari2 = sq.sqpari(a);
  System.out.println(
"Area of second square = " + sarea2);
  System.out.println("Parimeter of second square = " + pari2);
 
 int x = 10, y = 20;
  System.out.println("Length of first Rectangle = " + x);
    System.out.println("Breadth of first Rectangle = " + y);
  rectarea1 = rect.rectarea(x,y);
  rectpari1 = rect.rectpari(x,y);
  System.out.println(
"Area of first Rectangle = " + rectarea1);
  System.out.println("Parimeter of first Rectangle = " + rectpari1);
  x = 
15;
  y =
 25;
  System.out.println("Length of second Rectangle = " + x);
  System.out.println("Breadth of second Rectangle = " + y);
  rectarea2 = rect.rectarea(x,y);
  rectpari2 = rect.rectpari(x,y);
  System.out.println(
"Area of second Rectangle = " + rectarea2);
  System.out.println("Parimeter of first Rectangle = " + rectpari2);
  }
}

Descriptions of the program:

Object : Objects are the basic run time entity or in other words object is a instance of a class . An object is a software bundle of variables and related methods of the special class. In the above example the sq is the object of square class and rect is the object of the rectangle class. In real-world objects share two characteristics: They have all state and behavior. For example, The squares have state such as : sides and behaviors such as its areas and perimeters. Rectangles have state such as: length, breadth and behavior such as its areas and perimeters. A object implements its behavior with methods of it's classes. A method is a function (subroutine) associated with an object. 

Syntax for the Object : 

class_name object_name = new class_name();

Output of the program : 

C:\chandan>javac ObjectClass.java
C:\chandan>java ObjectClass
Side of first square = 20
Area of first square = 400
Parimeter of first square = 80
Side of second square = 30
Area of second square = 900
Parimeter of second square = 120
Length of first Rectangle = 10
Breadth of first Rectangle = 20
Area of first Rectangle = 200
Parimeter of first Rectangle = 60
Length of second Rectangle = 15
Breadth of second Rectangle = 25
Area of second Rectangle = 375
Parimeter of first Rectangle = 80

Constructor: Every class has at least one it's own constructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor.

In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In the Construct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor.

Here is the code of the program : 

class another{
  
int x,y;
  another(
int a, int b){
  x = a;
  y = b;
  }
  another(){
  }
 
 int area(){
  int ar = x*y;
 
 return(ar);
  }
}
public class Construct{
 
  public static void main(String[] args)
 {
 another b = 
new another();
 b.x =
 2;
 b.y =
 3;
 System.out.println("Area of rectangle : " + b.area());
 System.out.println("Value of y in another class : " + b.y);
 another a = 
new another(1,1);
 System.out.println("Area of rectangle : " + a.area());
 System.out.println("Value of x in another class : " + a.x);
 }
}

Output of the program : 

C:\chandan>javac Construct.java

C:\chandan>java Construct
Area of rectangle : 6
Value of x in another class : 3
Area of rectangle : 1
Value of x in another class : 1

Constructor Overloading: Here, you will learn more about Constructor and how constructors are overloaded in Java. This section provides you a brief introduction about the Constructor that are overloaded in the given program with complete code absolutely in running state i.e. provided for best illustration about the constructor overloading in Java.

Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor.

Whenever we assign the name of the method same as  class name. Remember this method should not have any return type. This is called as constructor overloading. 

We have made one program on a constructor overloading, after going through it the concept of constructor overloading will get more clear. In the example below we have made three overloaded constructors each having different arguments  types so that the JVM can differentiates between the various constructors.

The code of the program is given below:

public class ConstructorOverloading{
  public static void main(String args[]){
  Rectangle rectangle1=new Rectangle(2,4);
  int areaInFirstConstructor=rectangle1.first();
  System.out.println(" The area of a rectangle in 
  first constructor is :  " 
+ areaInFirstConstructor);
  Rectangle rectangle2=new Rectangle(5);
  int areaInSecondConstructor=rectangle2.second();
  System.out.println(" The area of a rectangle in 
   first constructor is :  " 
+ areaInSecondConstructor);
  Rectangle rectangle3=new Rectangle(2.0f);
  float areaInThirdConstructor=rectangle3.third();
  System.out.println(" The area of a rectangle in first 
  constructor is :  " 
+ areaInThirdConstructor);
  Rectangle rectangle4=new Rectangle(3.0f,2.0f);
  float areaInFourthConstructor=rectangle4.fourth();
  System.out.println(" The area of a rectangle in first 
  constructor is :  " 
+ areaInFourthConstructor);
  }
}

class Rectangle{
  int l, b;
  float p, q;
  public Rectangle(int x, int y){
  l = x;
  b = y;
  }
  public int first(){ 
  return(l * b);
  }
  public Rectangle(int x){
  l = x;
  b = x;
  }
  public int second(){
  return(l * b);
  }
  public Rectangle(float x){
  p = x;
  q = x;
  }
  public float third(){
  return(p * q);
  }
  public Rectangle(float x, float y){
  p = x;
  q = y;
  }
  public float fourth(){
  return(p * q);
  }
}

Output of the program is given below:

C:\java>java ConstructorOverloading
The area of a rectangle in first constructor is : 8
The area of a rectangle in first constructor is : 25
The area of a rectangle in first constructor is : 4.0
The area of a rectangle in first constructor is : 6.0