Java is an object-oriented programming language that allows you to split complex problems into smaller sets by creating objects. These objects have two properties:
Java is an object-oriented programming language (OOP). In this tutorial, you will get acquainted with object-orientation and how to create custom classes and objects in the program.
- state of
- Behavior
Let’s give some examples:
1- A lamp is an object
- It can be on or off.
- You can turn the lamp on and off. (Behavior)
2- A bicycle is an object
- It has gears, two wheels, a number of gears, etc. (mode)
- It can be braked, accelerated, changed gears and so on. (Behavior)
Java class
Before you can create objects in Java, you must define a class.
A class is a design for an object. We can imagine the classroom as a design (prototype) of a house. It contains all the details about floors, doors, windows and so on. Based on these descriptions, we build the house. The house is an object.
Since many houses are built with the same description, we can create many objects from one class.
How to define a class in Java?
Here is how to define a class in Java:
- class ClassName {
- // variables
- // methods
- }
Another example:
- class Lamp {
- // variable instance
- private boolean isOn;
- // method
- public void turnOn () {
- isOn = true;
- }
- // method
- public void turnOff () {
- isOn = false;
- }
- }
Here, we defined a class called Lamp.
The class has a sample variable (variable defined within the class) isOn and two methods (turnOn) and turnoff (). These variables and methods defined in a class are called class members.
In the above program, pay attention to two keywords, private and public. These terms define the level of access, which we will explain in later tutorials. For now, just remember:
- The private keyword privatizes variables and methods that are only accessible within the same class.
- The public keyword generalizes variables and methods that can be accessed outside the classroom.
In the above program, the variable isOn is private, while the methods turnOn () and turnoff () are public.
If you try to access private members from outside the classroom, the compiler will show an error.
Java objects
When the class is defined, only the object properties are defined. No memory or storage space is allocated.
You must create an object to access the defined members of the class. We created an object from the Lamp class in the following code snippet.
- class Lamp {
- boolean isOn;
- void turnOn () {
- isOn = true;
- }
- void turnOff () {
- isOn = false;
- }
- }
- class ClassObjectsExample {
- public static void main (String [] args) {
- Lamp l1 = new Lamp (); // create l1 object of Lamp class
- Lamp l2 = new Lamp (); // create l2 object of Lamp class
- }
- }
This program creates two objects l1 and l2 from the Lamp class.
How to access members?
You can access members (functions and variables) using (.). For example,
l1.turnOn ();
This statement invokes the turnOn () method in the Lamp class for object l1.
When you call a method using the above expression, all expressions in the body of the method turnN () are executed. Then, control the program under the phrase
l1.turnOn ()
Jumps.
Similarly, the sample variable can be accessed:
l2.isOn = false;
It is important to note that private members are only accessible within the classroom. If the code
l2.isOn = false;
In the main () function (outside the Lamp class), the compiler shows an error.
Example: Classes and Objects in Java
- class Lamp {
- boolean isOn;
- void turnOn () {
- isOn = true;
- }
- void turnOff () {
- isOn = false;
- }
- void displayLightStatus () {
- System.out.println (“Light on?” + IsOn);
- }
- }
- class ClassObjectsExample {
- public static void main (String [] args) {
- Lamp l1 = new Lamp (), l2 = new Lamp ();
- l1.turnOn ();
- l2.turnOff ();
- l1.displayLightStatus ();
- l2.displayLightStatus ();
- }
- }
Output
Light on? true
Light on? false
In the above program,
- The Lamp class is created.
- The class has one instance variable isOn and three methods (turnOn (), turnoff and displayLightStatus ().
- Two objects l1 and l2 of the Lamp class are created in the main () function.
- Here, the turnOn () method is called using the object l1:; () l1.turnOn
- This method sets the isOn variable of object l1 to true.
- And the turnoff () method is called using the object l2:; () l2.turnOff
- This method sets the isOff variable of object l2 to false.
- Finally,; () l1.displayLightStatus phrase Light on? Prints true. Because the isOn variable is set to true for object l1.
- And l2.displayLightStatus; The phrase Light on? Prints False. Because the isOn variable for object l2 is set to false.
When objects are initializing, each object contains its own version of variables and methods. For example, the isOn variable is different for objects l1 and l2.