title

Total 2 Lessons available

Classes and Objects in Java

Classes and Objects in Java

Classes in Java

Classes are considered blueprints for an object. It consists of the behavior, and attributes of an object. Classes and objects are the basis of all object-oriented programming concepts. Class doesn't occupy any memory until it is instantiated. The class contains different methods and different variables in it. Generally, a class in java contains methods, variables, constructors, and nested classes. A class represents a set of properties followed by all objects in common. A class is generally enclosed in {}.

Syntax

Below is the syntax to be followed to create a class in java:

access_modifier class<class_name> {
    constructor;
    data member; 
    method; 
    nested class;
    interface;
}

Access modifier: Accesses modifiers are private, public, default, and protected.

  • Constructor: Constructors are special methods that are named the same as the class name and it is invoked whenever an object of the class is created. It is generally used for the initialization of variables
  • Data members/variables: A class can consist of data members of different data types.
  • Methods: A class consists of different methods which have different properties to be implemented.
  • Nested class: A class in itself can consist of a nested class.
  • Interface: This keyword when used while initiating a class is helpful for multiple inheritances in java.

Objects in Java

It represents real-time entities. It occupies memory. It is instantiated using a keyword new in java. It consists of state, behavior, and identity. Identify represents the unique name that is given to the object which is created. Behavior consists of the methods of the object. The state of the objects represents the attributes of the class, it basically represents the properties of the class. There are many ways in creating an object of a class.

Example

Below is an example of implementing both class and object in java:

public class Mandarin {
    // Instance Variables
    int cost;
    String color;
    double weight;
    String taste;

    // Constructor Declaration of Class
    public Mandarin(String color, int cost , double weight, String taste) {
        this.color = color;
        this.taste = taste;
        this.weight = weight;
        this.cost = cost;
    }

    // method 1
    public String getTaste() {
        return taste;
    }

    // method 2
    public int getCost() {
        return cost;
    }

    // method 3
    public double getWeight() {
        return weight;
    }

    // method 4
    public String getColor() {
        return color;
    }
    // overiding a method
    @Override
    public String toString() {
        return("I am eating a Mandarin of color "+ this.getColor()+
              "." + "\n Mandarin has the weight and cost as " +
              this.getWeight()+"kg, " + "Rs." +this.getCost()+ "."+ "\n The taste of Mandarin is "+ this.getTaste());
    }
    // Driver code
    public static void main(String[] args) {
        Mandarin orange1 = new Mandarin("orange", 20, 0.5, "sour");
        System.out.println(orange1.toString());
    }
}

The output obtained is as follows:

I am eating a Mandarin of color orange.
Mandarin has the weight and cost as 0.5kg, Rs.20.
The taste of Mandarin is sour

Conclusion

Classes and objects are considered building blocks of object-oriented concepts. Class is the blueprint of the object which has to be created. It consists of methods, data members, nested classes, etc. Object consists of a state, behavior, and identity of its own.