Java class example : Using classes in java


The objects are data and methods bundled together into a single logical software unit. The next question that arises in our mind is how objects get into our software system and how objects get created. At this point we get introduced to the most fundamental structure in object oriented programming, the classes.

A java class is a template or prototype which defines the outline of state and behavior for all the objects belonging to that class. An instance of a class is called as object and all the objects of a class will have common state and behavior.

A java class is to an object is like a blue print is to a house. Any number of houses can be constructed from a single blue print. In a similar way many objects can be created from a single class. A class outlines the properties of an object. Objects created from the same class show similar characteristics.

The attributes and actions can be assigned only after an object is created. When an object is created then the actual instance of the entity come into existence. Hence, a java class can be called as a conceptual model of an entity which is universal and non-specific.

Java class syntax:

class identifier
{
class body
}

Here identifier specifies the name of the class. Class body consists of data members and method definitions. Curly braces surround the class body.

A class is static, whereas the data contained in an object can be dynamic. In a class, attributes are set throughout the execution of the program.

Example:

class ExampleClass
{
//Data member declaration
char ch;
int num;
double decimalnum;

void exampleMethod1()
{
System.out.println(“Hello Java”);
}
void exampleMethod2()
{
System.out.println(“Javaeschool.com”);
}
}

In the above example, ExampleClass is the class name. The class body contains 3 data members and 2 member method implementations.

No comments:

Post a Comment