dsa

Java Class and Objects

Introduction :

In this tutorial, you will learn about object-oriented programming in Java and you will learn about Java classes and objects with the help of examples.

Java is an object-oriented programming language. It is based on the concept of objects. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :

Example of an Object : Dog

Java Object Example
fig: Java Object Example

Principles of Object-oriented Programming:

The focus of object-oriented programming is to break a complex programming task into objects that contain fields (to store data) and methods (to perform operations on fields).


Java Classes

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:

Syntax to declare class in java :


class 
{  
    //field  
    //method  
}

Example :


public class Circle 
{
	private double x, y; // centre of the circle
	private double r;    // radius of circle
	//Methods to return circumference and area
	public double circumference() 
	{
		return 2*3.14*r;
	}
	public double area() 
	{
		return 3.14 * r * r;
	}
}

Note : Here, we have created a class named Circle.

The class has three variables named x,y and r and two methods circumference() and area(). These variables and methods defined within a class are called members of the class.

In the above example, we have used keywords private and public. These are known as access modifiers. To learn more, visit Java Access Modifiers.

Java Objects

A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

For example, suppose City is a class then Bhopal,Nagpur,Dhanbad and so on can be considered as objects of City class.

Syntax to creating objects :


className object = new className();

Example :


City bhopal= new City();   //1st object

City nagpur= new City();   //2nd object

Note : Objects are used to access members of a class.

How to access members of a class ?

Objects are used to access members of the class. We can access members (call methods and access instance variables) by using the "." operator. For example,


class City 
{
	checkPopulation() 
	{
		//some code...
	}
}

// create object

City bhopal = new City();

// access method checkPopulation

bhopal.checkPopulation();     //This statement calls the checkPopulation() method inside the City class for the bhopal object.

We have mentioned the word method quite a few times. You will learn about Java methods in detail in the next chapter. Here's what you need to know for now: When you call the method using the above statement, all statements within the body of the checkPopulation() method is executed. Then, the control of the program jumps back to the statement following bhopal.checkPopulation();

Example: Java Class and Objects


//Java Program to illustrate how to define a class and fields  
//Defining a Student class. 
 
class Student
{  
	//defining fields  
	int id;     //field or data member or instance variable  
	String name;  
	//creating main method inside the Student class 
	public void display(int id1, String name1) 
	{
		System.out.println("id ="+id1+" and Name ="+name1);
	}
	public static void main(String args[])
	{  
		//Creating an object or instance  
		Student s1=new Student();     //creating an object of Student  
  		//Printing values of the object  
  		s1.display(1,"CodeMistic");
 	}  
}
Output :

id =1 and Name =CodeMistic

In the above program,

  1. We have created a class named Student.
  2. The class has two instance variable id and name one method display() .
  3. Inside the Main class, we have created one object s1 of the Student class.
  4. We then use the s1 object to call display().

Note: The variables defined inside a class are known as instance variables for a reason. When an object is created, it is called an instance of the class.

Each instance contains its own copy of the variables defined inside the class. Hence, known as instance variables.