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 :
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).
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:
class
{
//field
//method
}
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.
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.
className object = new className();
City bhopal= new City(); //1st object
City nagpur= new City(); //2nd object
Note : Objects are used 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();
//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,
display()
.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.