Classes and Objects:

Classes and Objects are the fundamental concepts in Object Oriented Programming. They are the building blocks for creating and organizing code in a structured and modular way.

Class:

Class is a template / blueprint which stores the propeties/attributes and behaviors of objects. Class is a user-defined data type.

Object:

Object is an entity which has its properties and behavior. For example, car is an object. It has properties like its color, weight, number etc. and behavior like it can stop, it can accelerate etc.
  • Properties of objects : data members declared inside the class.
  • Behavior of objects : members functions declared inside the class.

So, properties and behaviors are members of class and declared inside the class.


The Dot Operator:

The dot operator (.) is used to access properties of object. When we create an object of a class and we want to access variables or methods of the class, we can access them using the dot operator. We will see the usage of dot operator in the example given below.

Creating Object:

Objects are created using the new keyword followed by the class constructor. The constructor initializes the object's state.

class.java
public class Main {

  String name = "John";
  int age = 30;

  public static void main(String[] args) {

    Main obj = new Main();     

    System.out.println("Name: " + obj.name);
    System.out.println("Age: " + obj.age);
  }
}

Note :

  • The name of the Java class should start with a capital letter.
  • The name of the java class should match the file name.


Multiple Objects:

We can create multiple objects of the same class.

class.java
public class Main {

  String name = "John";
  int age = 30;

  public static void main(String[] args) {

    Main obj1 = new Main();     
    System.out.println("Name: " + obj1.name);
    System.out.println("Age: " + obj1.age);


    Main obj2 = new Main();     
    System.out.println("Name: " + obj2.name);
    System.out.println("Age: " + obj2.age);
  }
}

Multiple Classes:

When working with multiple classes in Java, you typically define each class in its own ".java" file.

Let's create two classes:
  • Person - represents a person with some name and age.
  • MainApp - entry point of the program and uses the Person class.

Person.java:

class.java
public class Main {
  private String name;
  private int age;

  public Main(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public void introduce() {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
  }
}

MainApp.java

class.java
public class MainApp {
    public static void main(String[] args) {
        // Create two Person objects
        Person person1 = new Person("Leah", 20);
        Person person2 = new Person("Gotti", 22);

        person1.introduce();
        person2.introduce();
    }
}

To compile and run this program:

  • Compile the both classes by running "javac Person.java MainApp.java".
  • Run the program, "java MainApp".

Changing the values of attributes:

We can change the value of variables in our main method by accessing it using dot operator.

class.java
public class Main {

  String name = "John";
  int age = 30;

  public static void main(String[] args) {

    Main obj = new Main();     
    obj.name = "Andrew";
    obj.age = 20;
    System.out.println("Name: " + obj.name);
    System.out.println("Age: " + obj.age);

  }
}

Unchangeable values of attributes:

If we do not want to change the values of our attributes, we declared them using final keyword.

class.java
public class Main {

  final String name = "John";
  final int age = 30;

  public static void main(String[] args) {

    Main obj = new Main();     
    obj.name = "Andrew";
    obj.age = 20;
    System.out.println("Name: " + obj.name);
    System.out.println("Age: " + obj.age);

  }
}

This code will give you error.