Classes and Object in Java

Advertisements

Java Classes and Object

Class is an encapsulation of data and coding. Class is userdefined datatype. Variables declared in class are called Data Members. Functions defined in class are called Member Functions.

Declaration of Class

Declaration of class must start with the keyword class followed by the class name and class members are declared within braces.

Syntax for declaration of class


	class class-name
	{
		datatype var1;
		datatype var2;
		- - - - - - - - - -
		datatype varN;

		method1();
		method2();
		- - - - - - - - - -
		methodN();
	}

Example for declaration of class


	class Employee
	{
		int Id;
		char Name[25];
		int Age;
		long Salary;

		void GetData()
		{
			-----------
			//code for GetData()
			-----------
		}
		void PutData();
		{
			-----------
			//code for PutData()
			-----------
		}
	}

Instantiation of objects OR Accessing the class members

Instantiation of object means, create an object of class to access its members. Object is a variable of class type. Class members are accessed using the dot operator(.) between class's object and class's member name.

Synatx of creating object in java


Class-Name obj = new Class-Name();

Example of creating object in java


	Employee Emp = new Employee();

Defining Class Member Functions

Syntax of defining class member function


	return-type function-name(argument list)
	{
		- - - - - - - - - -
		body of function
		- - - - - - - - - -
	}


return-type : Type of value function will return.
function-name : Can be any valid Java identifier.
argument list : Represents the type and number of value function will take, values are sent by the calling statement.

Example of Java class


	//Employee.java

    import java.util.Scanner;

    class Employee
    {
        int Id;
        String Name;
        int Age;
        long Salary;

        void GetData()           // Defining GetData()
        {

            Scanner sc = new Scanner(System.in);

            System.out.print("\n\tEnter Employee Id : ");
            Id = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Employee Name : ");
            Name = sc.nextLine();

            System.out.print("\n\tEnter Employee Age : ");
            Age = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Employee Salary : ");
            Salary = Long.parseLong(sc.nextLine());

        }

        void PutData()           // Defining PutData()
        {

            System.out.print("\n\tEmployee Id : "+Id);
            System.out.print("\n\tEmployee Name : "+Name);
            System.out.print("\n\tEmployee Age : "+Age);
            System.out.print("\n\tEmployee Salary : "+Salary);

        }

        public static void main(String args[])
        {

            Employee E = new Employee();// Creating Object

            E.GetData();          // Calling GetData()
            E.PutData();          // Calling PutData()

        }
    }

    Output :

                Enter Employee Id : 1
                Enter Employee Name : Kumar
                Enter Employee Age : 29
                Enter Employee Salary : 45000

                Employee Id : 1
                Employee Name : Kumar
                Employee Age : 29
                Employee Salary : 45000

In the above program, we have defined two member functions GetData() and PutData() within class. Statement 3 is creating an object E of Employee class. Statement 4 and 5 is invoking/calling member function through the object E of Employee class.

Advertisement