Class

This section explores the concept of
a class in reference to object oriented programming techniques. A class defines
the properties of objects and methods used to control an object's
behavior. In terms of object-oriented programming, the class is like a template
from which an instance (object) of a class is created at run time.
Let us consider an example of a car.
Manufacturer made so many cars, all cars have their own unique state
but they have same behavior (you can learn about state and behavior
in the topic object.). To built a car, a blueprint is required that
specifies a perfect design and illustrates the vital things to manufacture a
car. This blueprint or the model can now be used to manufacture thousands
and thousands of cars ready to run on the roads.
The same concept is employed in the
object oriented techniques to create the objects through programming. The
blueprints used to create the objects are called classes. A class defines the
properties of the objects and the methods used to control an object's behavior. In terms of
object-oriented programming, a class is like a template from which an
instance of that class is created at run time.
After defining a class, it can be used to create objects by instantiating
the class. Each object occupies some memory to hold its instance variables
(i.e. its state).
The state of each object is separate from that of the others. After an
object is created, it can be used to get the desired functionality together with
its class.
Now
we will learn, how to define a class having methods, objects and declaration of method by a given example.
In the given example we create a main class "Classtest"
and also create
a class "Example" which is outside of the main class and define a
method "display" in it. Now create a class object "myExample"
of class "Example" and call display() method in main class. The class "Example" is the blue print of the class "Classtest".
Here is the Code of the Example :
Classtest.java
import java.lang.*;
public class Classtest{
public static void main( String args[] )
{
Example myExample = new Example();
myExample.display();
}
}
class Example
{
public void display(){
System.out.println( "\nIt's a example of class" );
}
}
|
Here is the Output of the Example :
C:\roseindia>javac Classtest.java
C:\roseindia>java Classtest
It's a example of class
|
Download This Example :

|