What is class and object?

What is class and object in Java


  1.   A class is a template from which objects are created. That is objects are instance of a class.
  2.  When you create a class, you are creating a new data-type. You can use this type to declare objects of that type.
  3.  Class defines structure and behavior (data & code) that will be shared by a set of objects
A class is declared by use of the class keyword. Classes may contain data and code both.
The general form of al class definition:
            class ClassName
            {
                        type instance variable1;
                        type instance variable2;
                       
                        type methodname1 (parameter list)
                        {
                                    body of method;
                        }
                        type methodname2 (parameter list)
                        {
                                    body of method;
                        }
             }

The data or variable are called instance variable. The code is contained within methods.
The method and variables defined within a class are called member of the class.

Example of Class


class Box
{
 double width;
 double height;
 double depth; 
}
class BoxDemo
{
 public static void main (String args[])
 {
  Box mybox = new Box ();
  double vol;
  mybox.width =10;
  mybox.height = 20;
  mybox.depth = 30;
  vol = mybox.width * mybox.height * mybox.depth;
  System.out.println (“Volume is: - “+vol);
 }
}


  • Each object contains its own copy of each variable defined by the class.
  • So, every Box object contains its own copy of the instance variables width, height and depth.
  • To access these variables, you will use the dot (.) operator. The dot operator links the name of the object with the name of an instance variable.

Declaring Object

•        Box mybox;     // declare ref. to object which contains null value.
•        mybox = new Box ();   // allocate a Box object.

General form of a new

            class var = new classname ();
            mybox = new Box ();
Where:
            class var =  variable of the class type.
            classname = name of the class.
The classname followed by parentheses specifies the constructor for the class.
A constructor defines what occurs when an object of a class is created.
Most classes explicitly define their own constructors within their class definition but if no explicit constructor is specified then java will automatically supply a default constructor.
This is the case with Box. This is default constructor.

Assigning Object Reference Variable

Box b1 = new Box ();
Box b2 = new b1;
After this executes, b1 and b2 will both refer to the same object.
The assignment of b1 to b2 did not allocate any memory or copy any part of the original object.
It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring,since they are the same object.

Introducing Methods

type name (parameter-list)
{
            body of method
}
Where:
Type : Specifies the type of data returned by the method.If the method does not return a value its ret urn type must be void.
Name: Specifies the name of the method.
Parameter-list: It is a sequence of type & identifiers pairs separated by commas.
Parameters are variables that receive the value of the argument passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.
Methods that have a return type other than void return a value to the calling routine using the following form of the return statement;
Return value;

Types of Methods

  1. Does not return value – void
  2. Returning a value
  3. Method which takes parameter

Next
This is the most recent post.
Previous
Older Post

0 comments:

Post a Comment

 
Top