Class & Object

Before discussing class & object, we will brief structure and it's limitation, that will give a quick perception to understand concept of class & object.

Need of class

'C' is procedural language. In 'C' function considered more important . More focus on the function than data. Structure is used to combine various data type into one newly defined data type. Structure provides a way to create a new data type. New data type can be used to create variable of that type in the same way we create variable of primitive data type.'struct' keyword used to create structure.


Example :

 #include<stdio.h> 
struct complex
{
int real;
int img;
};
void main()
{
struct complex comp;
comp.real=12;
comp.img=4;
}

Above program creating a new data type for complex number. Complex has two parts real and imaginary. In main function structure varible can be created using 'struct' keyword.

Extension of structure: class

Structure focus mainly on data, there is no provision for functions in structure. In C++ this feature added, which allow programmer to define function in structure plus data can also be declared private to secure from unwanted access from outside.

Example :

 #include<conio.h>
struct complex
{
int real;
int img;
void show()
{
cout<<"real:"<<"real;
cout<<""img:"<<"img;
}
void set(int r,int i)
{
real=r;
img=i;
}
};
void main()
{
struct complex comp;
comp.set(2,3);
comp.show();
}

Structure in C++ has capacity to define functions. C++ has introduced a new keyword 'class' to create a used defined data type. In structure data members are public by default( it can be accessed outside structure), but in class data members are private. Class has extra feature of constructor and destructor.


Class in Java

Java has borrowed OOP feature from C++ language. Class declaration is same in java as in C++. If a programmer knows 'C++' language then he would take less time to start programming in java. Class is can be defined in many ways, it is user defined data type, It is defined as template or its a collection of similar objects.

Syntax :

class <class_Name>
{
<access_specifier> <data_type> variable1;
<access_specifier> <data_type> variable2;
<access_specifier> <data_type> variable3;
<access_specifier> <data_type> variable4;
........................................
........................................
<return_type> <method_name> ( argument_list)
{
method body
}
<return_type> <method_name> ( argument_list)
{
method body
}
<return_type> <method_name> ( argument_list)
{
method body
}
}

Access specifier specify the accessibility of data members of class. By default all data member can be accessible in same package or public in same package. To make data member private, 'private' access specifier can be used. Any number of variable can be declared in class. If these data member declared with private keyword then it can't be accessed outside the class. This restriction on data access add feature of data hiding to OOP language. Methods are declared and defined in same way as it were in c or c++.


Example: Class discussed below is the example of complex number. Class has two private data real and imginary. To manipulate these data, only way left is using method. Method set is setting the value of these parameter and another method show printing the value of data member.


Example :


 // file: complex.java 
// www.jgyan.com
class complex
{
private int real;
private int img;

public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
comp1=new complex();
comp1.set(12,23);
comp1.show();
}
}