Modal Box

Class and Object

As we know java is an object oriented language so everything is surrounded class and object.
Class may defined in many ways like “Collection of similar types of elements” , “User Defined Data types” or “Template for variables and functions” etc.
Object is the real entity and may be defined as the “instance of class”.


In Java we can create the class as following :

  class MyClass
{
int x=100;
void display()
{
System.out.println("x="+x);
}
}

⇒ The above class has one class variable and one method but it does not have  the main method, so directly it cannot be run.
⇒ This class may be run by creating the object of this class into another class.

Object of the class can be created as following :

 MyClass obj= new MyClass();

// Here we are creating obj (object) of MyClass (class) .
⇒ When we call the function we may also pass the parameters and the function may return the value.

class Area
{
  int length=0;
  int breadth=0; //class variables

  void setData(int l, int b)
  {
    length=l;   //class variable initialized here by local variable
    breadth=b;
   }

  int areaCal()
  {
    int area=length*breadth;
    return area;
   }
}

class checkMain
{
  public static void main(String args[])
   {
     Area a= new Area(); // object is created here
     int l=20, b=10; // these values may be taken as input from user we will take that later.
     a.setData(l,b);
     int ar=a.areaCal();  //the areaCal Function return the value here.
     System.out.println("area is"+ar);
   }
}
   output: area is 200.

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial