Java Object and Classes

Objects in Java:

An object is an instance of a class.

Software objects also have a state and behavior just like real world object.

A software object’s state is stored in fields and behavior is shown via methods.

Real world object example can be dog and it’s state can be name,breed,color and the behavior can be barking,wagging,running etc.

So in software development, methods operate on the internal state of an object and the object to object communication can be done via method that means internal connection in oop language like java is done by methods.

Classes in Java:
A class is a blue print/template from which individual objects are created.

Method:

A method is basically a behavior. A class can contain many methods.In method where the logic is written,data is manipulated and all the actions are executed here.

A sample of a class is given below:

public class Dog {   //Dog is a class here
String breed;
int age;
String color;
String name;
void barking() //it is method
{
	
}
void hungry()    //it is another method
{
	
}
void sleeping()   //it is also another method
{
	
}

}

A class can contain any type of the following Java variables:

Local Variables:

Variables defined inside methods,constructors or blocks of code are called local variables.

Instance Variables(Non-Static Variables):
It is outside method but inside class.It can be accessed form inside any method,constructors or blocks of that particular class.

Class Variables(Static Variables):
Class variables are declared with in a class, outside a method, with the static keyword.

Constructors in Java:

The main rule of constructors is that they should have the same name as the class.A class can have more than one constructor.

Example of a constructor is given below:

public class Puppy {
	public Puppy()
	{
		
	}
	
	public Puppy(String name){
		//This constructor has one parameter, name
	}
	
	
}

 

Creating an Object:

A class provides the blueprints for objects. SO basically an object is created from class. In java, the new key word is used to create new objects.

There are three steps when creating an object from a class:

Declaration: A variable declaration with a variable name with an object type.

Instantiate: The ‘new’ key word is used to create the object.

Initialization: The ‘new’ keyword is followed by a call to a constructor.This call initializes the new object.

Example of creating an object is given below:

public class Puppyu {

	public Puppyu(String name)
	{
		//This constructor has one parameter, name
		System.out.println("Passsed name is:"+name);
	}
	
	
	public static void main(String []args)
	{
		//Following statement would create an object myPupppy
		Puppyu myPupppy=new Puppyu("koi");
	}
	
	
}

Output:

Passsed name is:koi

Accessing instance variables and methods:

Instance variables and methods are accessed via created objects.To access an instance variable the fully qualified path should be as follows:

/* First create an object */
ObjectReference=new Constructor();

/* Now call a variable as follows */
ObjectReference.variableName;

/* Now you can call a class method as follows */
ObjectReference.MethodName();

Example:

This example explains how to access instance variable and methods of class:

 

public class Pappu {

	int pappuAge;
	
	public Pappu(String name){
		//This constructor has one parameter, name
		
		System.out.println("Name chosen is: "+name);
	}
	
	public void setAge(int age)
	{
		pappuAge=age;
		
	}
	
	public int getAge()
	{
		System.out.println("Pappu's age is:"+pappuAge);
		return pappuAge;
	}
	
	
	public static void main(String []args)
	{
		/* Object creation */
		Pappu myPappu=new Pappu("tommyy");
		
		/* call class method to set pappu's age */
		myPappu.setAge(10);
		
		/* call another class method to get pappu's age */
		myPappu.getAge();
		
		/* You can acccess instance variable as follows as well */
		System.out.println("Variable value: "+myPappu.pappuAge);
	}
	
	
}

example:
d

Name chosen is: tommyy
Pappu's age is:10
Variable value: 10

Source file declaration rules:

package

import

class {}

Java Package:

In simple it is a way f categorizing the classes and interfaces.When developing applications in Java,hundreds of classes and interfaces will be written,therefore categorizing these classes is a must as well as makes life much easier.

Import statement:

import java.io.*

Example:

Employee.java

import java.io.*;

public class Employee{

String name;
int age;
String designation;
double salary;

//This is the constructor of the class employee
public Employee(String name)
{
this.name=name;
}

//Assign the age of the Employee to the variable age
public void emplAge(int emplyAge)
{
age=emplyAge;
}

//Assign the designation to the variable designation
public void emplDesignation(String emplyDesigna)
{
designation=emplyDesigna;
}

//Assign the salary to the variable salary

public void empSalary(double emplSalary)
{
	salary=emplSalary;
}

//Print the employee details now

public void printEmployee()
{
	System.out.println("Name: "+name);
	System.out.println("Age: "+age);
	System.out.println("Designation: "+designation);
	System.out.println("Salary: "+salary+"$");
}

}

EmployeeTest.java

import java.io.*;

public class EmployeeTest {

	public static void main(String args[])
	{
		//creating two object using construtcor
		Employee emplOne=new Employee("Zaki Live");
		Employee emplTwo=new Employee("John");
	
	
		//Invoking method for each object created
		emplOne.emplAge(20);
		emplOne.emplDesignation("Software Engineer");
		emplOne.empSalary(100000);
		emplOne.printEmployee();
	
		emplTwo.emplAge(18);
		emplTwo.emplDesignation("QA");
		emplTwo.empSalary(5000);
		emplTwo.printEmployee();
		}

}

To get output we have to use public main function and call it in EmployeeTest.java
so the outputs is:

Name: Zaki Live
Age: 20
Designation: Software Engineer
Salary: 100000.0$
Name: John
Age: 18
Designation: QA
Salary: 5000.0$

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.