Khurram's World - JAVA Repitition
 
 
..Islam and Religion
 .Computer & IT
  Web Designing TIPS
..HTML & DHTML
..Active Server Pages
..JAVA & JAVA Applets
..JAVA & VB Script
..CGI & Perl
..Electronic Commerce
..Web Sites & Emails
 
Pakistan News Service
Bay Banner
 
 
 
Object and Classes  

Objective
Learn about:

  • Difference between Classes and Objects
  • How to create an instance of a class
  • What are Methods and how they are created
  • Using Constructors
  • Concept

Class  
“A Class is a blueprint or a template of an object. It reserves no memory space for the data.”  

Classes define objects. A class is the model or the pattern from which an object is created. Consider the blueprint created by an architect when designing a house. The blueprint defines the important characteristics of the house like walls, windows, doors, etc. once the blueprint is created several houses can be created using the same blueprint. The houses built from the same blueprint will be different in way that they are physically at different places, have different furniture and different people live in them. However, in many ways they are the same house, since layout of the rooms and crucial characteristics are the same.

A class is a blueprint of an object. It defines the type of data that will be held in an object and defines the code for the methods. A class is not an object any more than a blueprint is a house. After creating the class we can create an object of that class. The process of creating an object is called instantiation. Every object is the instance of a particular class; we can instantiate several objects from the same class. They are the same type of object with the same methods, but each object is unique in the sense that each object has its own data with possibly different values.


Object

“An object is an instance of a class, it has variables and methods. The variables define the state of the object and the methods defines the behavior of the object”.

A class provides the definition for a particular type of object. It defines the data inside an object, the specifies of that object’s creation, and the functionality provided by that object to act on its own data. A class is therefore a template, objects are constructed based on there class model, much like a building is constructed from an architectural drawing, many buildings can be built from the same blueprint, yet each building is an object of its own.

It should be noted that the class defines, what an object is but is not itself an object. There is only one copy of a class definition in a program, but there can be several objects that are instances of that class.

Methods, define the operation that can be carried out on objects. In short methods define what the class does. Thus all methods in JAVA programming language must belong to a class.

Syntax

class classname {
        type instance-variable;
        type method-name(parameter-list) {
                //….. method body
        }
}

Key Concepts

Variables defined within a class are called instance variables because each instance of the class contains its own copy of the variables. Thus the data for one object is separate and unique from the data of another.

A class defines a new data type. Once defined, this new data type can be used to create an object of that type.

Example

class Box {
        double width;
        double height;
        double depth;
}

class BoxDemo {
        public static void main(String args[]) {
                Box mb = new Box();
                double vol;

                mb.width = 10;
                mb.height = 12;
                mb.depth = 13;

                vol = mb.width * mb.height * mb.depth;
                System.out.println(“Volume is: “ + vol);
        }
}

Examples of a class that contains two objects

class Box {
        double width;
        double height;
        double depth;
}

class BoxDemo {
        public static void main(String args[]) {
                Box mb1 = new Box();
                Box mb2 = new Box()
                double vol;
                mb1.width = 10;
                mb1.height = 12;
                mb1.depth = 13;
                vol = mb1.width * mb1.height * mb1.depth;
                System.out.println("Volume is: " + vol);

                mb2.width = 20;
                mb2.height = 18;
                mb2.depth = 19;
                vol = mb2.width * mb2.height * mb2.depth;
                System.out.println("Volume is: " + vol); 
        }
}

Example of class that contains a method

class Box {
        double width;
        double height;
        double depth;

void volume () {
        System.out.println(width * height * depth);  
        }
}

class BoxDemo {
        public static void main(String args[]) {
                Box mb1 = new Box();
                mb1.width = 10;
                mb1.height = 12;
                mb1.depth = 13;

                mb.Volume();
        }
}


Methods

A method is a group of programming language statements that are given a name. A method is associated with a particular class. Each method has a method definition that specifies code that gets executed when the method is invoked. When a method is called the flow is transferred to that method, one by one the statements of that method are executed. When the method is done the control is shifted to that location where the call was made from and the execution continues.

Syntax
<return-type> <method-name> (parameter-list) {
        statement-list
}
//<return-type> indicates the type of value returned by the method

Methods are normally categorized in two ways:

  • Methods that do not return a value

  • Methods that return a value

Methods that do not return a value

The methods that do not return a value are normally considered as procedures. If a method does not return a value, its return type must be declared void.

Example
void abc() {
        System.out.println(“Hello Every One”);
}

Methods that return value

Methods that have a return type other then void return a value to the calling routine using the following form of the return statement:

        return value;

Syntax

<return-type> <method-name> (parameter-list) {
       
return value;
}

Example

double volume() {
       
return width*depth*height;
}

An example of a method that does not return value:

class Box {
        double width;
        double height;
        double depth;
        void volume() {

                System.out.println(width*height*depth);
       
}
}

class BoxDemo {
       
public static void main(String args[]) {
       
Box mb1 = new Box();
       
mb1.width = 10;
       
mb1.height = 12;
       
mb1.depth = 13;

        System.out.println(“Volume is: “);
       
mb1.volume();
       
}
}

An example of a method that returns a value

class Box2 {
        double width;
        double height;
        double depth;
        void volume() {

                return width*height*depth;
       
}
}

class BoxDemo2 {
       
public static void main(String args[]) {
       
Box2 mb1 = new Box2();
       
double vol;
       
mb1.width = 10;
       
mb1.height = 12;
       
mb1.depth = 13;

        vol = mb1.volume();
            System.out.println(“Volume is: “+vol); 
       
}
}

Key Concepts

  • The type of data returned by the method must be compatible with the return type specified by the method, for example if the return type of a method is Boolean, you cannot return an integer.

  • The variable receiving the value returned by the method must also be compatible with the return type specified for the method

  • A variable declared inside a method is local to that method.

Parameterized Methods

A parameter is a variable by a method that receives a value when the method is called. The parameter list in the header of a method specify the type of the values that are passed and the name by which the called method will refer to the parameters in the method definition.

In the method definition, the names of the parameters accepted are called Formal Parameters. In the invocation the values passed to the methods are called Actual parameters. A method invocation and definition always specify the parameter list in the parenthesis after the method name. If there are no parameters an empty set of parenthesis is used.

Example

class Box3 {
        double width;
        double height;
        double depth;
        void volume() {

        return width*height*depth; 
       
}

        void setDim(double v, double h, double d) {
       
        width = w;
       
        height = h;
       
        depth = d;
       
}
}

class BoxDemo3 {
       
public static void main(String args[]) {
       
Box3 mb1 = new Box3();
       
mb1.setDim(12,13,14);
       
vol = mb1.volume();
       
System.out.println(“Volume is: “+vol);
       
}
}


Constructors

Constructors are used to initialize an object immediately upon creation. JAVA allows objects to initialize when they are created; the automatic initialization is performed through the use of constructors.

Key Concepts

  • A constructor has the same name as the class in which it resides and is syntactically similar to a method.

  • Once defined a constructor is automatically called immediately after the object is created, before the new operator completes.

  • Constructors have no return type, not even void. This is because implicit return type of a class constructor is the class type itself.

Example

Class Box4 {
        double width;
        double height;
        double depth;

        //constructor
        Box4() {
                width = 10;
                height = 12;
                depth = 15;
        }

        double volume () {
                return width * height * depth;
        }
}

class BoxDemo4 {
        public static void main(String args[]) {
                Box4 mb1 = new Box4();
                double vol ;

        vol = mb1.volume();
        System.out.println(“Volume is: “ + vol);
        }
}

 

Parameterized Constructors

Class Box5 {
        double width;
        double height;
        double depth;

        //constructor
        Box4(double w, double h, double d) {
                width = w;
                height = h;
                depth = d;
        }

        double volume () {
                return width * height * depth;
        }
}

class BoxDemo5 {
        public static void main(String args[]) {
                Box5 mb1 = new Box5(10,12,15);
                double vol ;

        vol = mb1.volume();
        System.out.println(“Volume is: “ + vol);
        }
}

 

Static Methods

A static method is not referenced through a particular instance of a class but through the class itself. You don’t have to instantiate an object of a class to invoked a static method, because static methods do not operate in the context of a particular object. They cannot reference instance variables.  

Key Concepts

  • They can only call other static methods

  • They must only access static data.

  • They cannot refer to this or super in any way.

 

Example

class UseStat {
        static int a = 3;
        static int b;

        // static block
        static {
                System.out.println(“Static Block initialized”);
                b = a * 4;
        }

//static method
        static void meth (int x) {
                System.out.println(“x is: “ + x);
                System.out.println(“a is: “ + a);
                System.out.println(“b is: “ + b);

}

public static void main(String args[]) {
meth(42);

}

}

 

Passing Objects to Methods

Example

class Test {
        int a, b;  

        Test (int i, int j) {
                a = i;
                b = j;
        }

        boolean equals (Test o) {
                if (o.a == a && o.b == b)
                        return true;
                else
                        return false;
        }
}

class PassOb {
        public static void main(String args[]) {
                Test ob1 = new Test (15, 13);
                Test ob2 = new Test (15, 13);
                Test ob3 = new Test (-1, -1);
                System.out.println(“ob1 == ob2 “ + ob1.equals(ob2));
                System.out.println(“ob1 == ob3 “ + ob1.equals(ob3));
        }
}

 

The this Keyword

‘this’ keyword is used inside any method to refer to the current object. ‘this’ is always a reference to the object on which the method was invoked.

//…..use this to resolve the namespace collision:
box(double width, double height, double depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
}