DED9

Teaching Classes And Objects In Java (In Very Simple Language)

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.

 

Let’s give some examples:

1- A lamp is an object

2- A bicycle is an object

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:

  1. class ClassName {
  2. // variables
  3.   // methods
  4. }

Another example:

  1. class Lamp {
  2.   // variable instance
  3. private boolean isOn;
  4.   // method
  5. public void turnOn () {
  6. isOn = true;
  7. }
  8. // method
  9. public void turnOff () {
  10. isOn = false;
  11. }
  12. }

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:

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.

  1. class Lamp {
  2. boolean isOn;
  3. void turnOn () {
  4. isOn = true;
  5. }
  6. void turnOff () {
  7. isOn = false;
  8. }
  9. }
  10. class ClassObjectsExample {
  11. public static void main (String [] args) {
  12. Lamp l1 = new Lamp (); // create l1 object of Lamp class
  13. Lamp l2 = new Lamp (); // create l2 object of Lamp class
  14. }
  15. }

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.

C: \ Users \ Mr \ Desktop \ java-method-working.jpg

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

  1. class Lamp {
  2. boolean isOn;
  3. void turnOn () {
  4. isOn = true;
  5. }
  6. void turnOff () {
  7. isOn = false;
  8. }
  9. void displayLightStatus () {
  10. System.out.println (“Light on?” + IsOn);
  11. }
  12. }
  13. class ClassObjectsExample {
  14. public static void main (String [] args) {
  15. Lamp l1 = new Lamp (), l2 = new Lamp ();
  16. l1.turnOn ();
  17. l2.turnOff ();
  18. l1.displayLightStatus ();
  19. l2.displayLightStatus ();
  20. }
  21. }

Output

Light on? true

Light on? false

In the above program,

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.

Exit mobile version