JAVA

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Objects And Classes Of Java

Java is an object-oriented programming (OOP) language. In this article, you'll be introduced to OOP and how you can create custom class and objects in your Java program.
Java is an object-oriented programming language. It allows you to divide complex problems into smaller sets by creating objects.
These objects share two characteristics:
  • state
  • behavior
  • Let's take few examples:

    1.Lamp is an object

    2.Bicycle is an object

    You will learn about 3 main features of an object-oriented programming: data encapsulation, inheritance and polymorphism in later chapters. This article will focus on class and objects to keep things simple.

    Java Class

    Before you create objects in Java, you need to define a class.
    A class is a blueprint for the object.
    We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
    Since, many houses can be made from the same description, we can create many objects from a class.

    How to define a class in Java?

    Here's how a class is defined in Java:
    class ClassName {
       // variables
       // methods
    }
    

    Here's an example:
    class Lamp {
    
      // instance variable
      private boolean isOn;
    
      // method
      public void turnOn() {
        isOn = true;
      }
    
      // method
      public void turnOff() {
      	isOn = false;
      }
    }
    

    --@ Here, we defined a class named Lamp.

    --@ The class has one instance variable (variable defined inside class) isOn and two methods turnOn() and turnOff(). These variables and methods defined within a class are called members of the class.

    --@ Notice two keywords, private and public in the above program. These are access modifiers which will be discussed in detail in later chapters. For now, just remember:

    --@ The private keyword makes instance variables and methods private which can be accessed only from inside the same class.

    --@ The public keyword makes instance variables and methods public which can be accessed from outside of the class.

    --@ In the above program, isOn variable is private whereas turnOn() and turnOff() methods are public.

    --@ If you try to access private members from outside of the class, compiler throws error.

    Java Objects

    When class is defined, only the specification for the object is defined; no memory or storage is allocated.
    To access members defined within the class, you need to create objects. Let's create objects of Lamp class.
    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 of class Lamp.

    How to access members?

    You can access members (call methods and access instance variables) by using . operator. For example,
    l1.turnOn();
    

    --@ This statement calls turnOn() method inside Lamp class for l1 object.

    --@ We have mentioned 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 turnOn() method are executed. Then, the control of program jumps back to the statement following l1.turnOn();



    --@ Similarly, the instance variable can be accessed as:
    l2.isOn = false;
    


    It is important to note that, the private members can be accessed only from inside the class. If the code l2.isOn = false; lies within the main() method (outside of the Lamp class), compiler will show error.

    Example: Java Class and Objects
    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();
      }
    }
    
    


    When you run the program, the output will be:
    Light on? true
    Light on? false
    

    In the above program,
  • Lamp class is created.
  • The class has an instance variable isOn and three methods turnOn(), turnOff() and displayLightStatus().
  • Two objects l1 and l2 of Lamp class are created in the main() function.
  • Here, turnOn() method is called using l1 object: l1.turnOn();
  • This method sets isOn instance variable of l1 object to true.
  • And, turnOff() method is called using l2 object: l2.turnOff();
  • This method sets isOff instance variable of l2 object to false.
  • Finally, l1.displayLightStatus(); statement displays Light on? true because isOn variable holds true for l1 object.
  • And, l2.displayLightStatus(); statement displays Light on? false because isOn variable holds false for l2 object

  • --@ Note, variables defined within a class are called instance variable for a reason.

    --@ When an object is initialized, it's called an instance.

    --@ Each instance contains its own copy of these variables. For example, isOn variable for objects l1 and l2 are different.