Thursday 6 December 2018

classes in java | java classes and object - Online Help

Class in Java:

           A class is an important element of object-oriented programming (OOP). The class is a user-defined data type. Each class has its own data items and functions. The data items are called variable names and the functions are called methods. Methods provide the logic that constitutes the behavior defined by a class.


           Each method can specify parameters that represent information the method requires to perform its task correctly. A method call supplies values called arguments for the method's parameters.
           A class is a collection of objects of similar type. Once a class is defined, any number of objects can be produced which belong to that class.

General Format of class:
class classname
{
datatype varname 1;
datatype varname 2;
-------------------
-------------------
datatype varname n;
returntype method_name1 (parameter_list)
{
// body of the method
}
returntype method_name2 (parameter_list)
{
// body of the method
}
----------------------------------------
----------------------------------------
returntype method_name n(parameter_list)
{
// body of the method
}

Where,
class - keyword
returntype - any valid java data type
methodname - name of the method


Declaring data members:

We can add a variable in a class it must be defined inside the class. The general format is:
class classname
{
datatype varname 1;
datatype varname 2;
-------------------
-------------------
datatype varnamen;
}

Example:
class student
{
int rollno;
char name;
int marks;
}

In the following example,create a class called student and it involves three data members:rollno,name,mark.


Defining Methods:

Functions that are declared to be part of a class are called methods. Methods are declared inside the boy of the class after the declaration of the data members. The general format of defining a method is as follows:

 returntype method_name(parameter_list)
                                                   {
//body of the method
                                                    }



returntype is primitive type i.e.any valid java's data type
Methodname is name of the method
parameter_list is list of arguments or parameters
body of the method is statements

Example:
class employee
{
int emp_id,emp_sal,emp_hra;
void read(int x,int y,int z)
{
emp_id=x;
emp_sal=y;
emp_hra=z;
}
int net_salary()
{
int n_salary=emp_sal+emp_hra;
}
}

Int the above example,this declaration defines a class called employee.It involves three data members:emp_id,emp_sal,emp_hra and two methods:read() and net_salary.

Creating objects:

A class can be used to create an instance of the class called an object.Creating object means allocating a memory space for all the objects.An object is created via the new operator.The syntax of defining an object is as follows:
                                   classname object_name;
                                 object_name= new classname();

In the above syntax is equivalent to,

                                 classname object_name=new classname();

Example:
employee emp1;
emp1=new employee();

is equivalent to

employee emp1= new employee();

We can create more number of object as folows:

employee emp1=new employee();
employee emp2=new employee();

is equivalent to

employee emp1=new employee();
employee emp2=new emp1;


Accessing class members:

Every object has its own number of variable and methods.Once an object of the class has been created,the source file can be access the members by using dot(.) operator.The syntax of the accessing the class member is,

                                                  objectname.variablename;

Example:
employee emp1=new employee();
emp1.empid=1001;

The methods are accessed using the dot(.) operator.The operator whose method is called in on the left side of the dot(.) , while the name of the method and its arguments are on the right side.The general form is
                                       
                                              objectname.methodname(parameter_list);

Example:
emp2.read(1002,8000,800);

When the read() is invoked the arguments passed to it or copied to the respective parameter of the method,which are assign to three variable of a class.

Program
class dotdemo
{
int x,y,z;
public void sum()
{
z=x+y;
}
public void show()
{
System.out.println("The answer is:"+z);
}
}

class demo1
{
public static void main(String args[])
{
dotdemo object=new dotdemo();
dotdemo object2=new dotdemo();
object.x=10;
object.y=15;
object2.x=5;
object2.y=10;
object.sum();
object.show();
object2.sum();
object2.show();
}
}
             Guys, i hope you guys understand the classes in java and if you guys want to know about system class in java,then click the below link:
https://beingcomputeracy.blogspot.com/2018/12/system-class-in-java.html






No comments:

Post a Comment