Java – Classes and Objects

Java is a computer language that focuses on objects. In Java, everything is linked to classes and objects, as well as their characteristics and methods. We can build objects in Java. To construct an object, the object must first be declared in the programme, which is accomplished by defining a class. A class is a piece of code that defines a new object type. The properties and methods of an object are specified within a class, and the attributes and methods of an object are decided by the class in which it is formed. Let us start with Java – Classes and Objects.

Create Class

In Java, the class keyword is used to construct a class. The keyword is followed by the class name and a pair of curly braces. All of its characteristics and techniques are included within the braces:

Syntax

//defining a class
class className {
  access_specifier attribute;
  access_specifier method;
};


access_specifier: It specifies how class characteristics and methods may be accessed. In Java, there are four different types of access specifiers.

  • public
  • protected
  • private
  • default: When no access_specifier is mentioned

The access to members granted by each access specifier is listed in the table below.

Access Specifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N

Create Object

To create an object, assign the new keyword followed by the class name to a variable (object name). The dot. operator is used to access a class member. Please note the following syntax:

Syntax

//creating an object
className objectName = new className();

//access class member (attributes and methods)
objectName.attribute
objectName.method(parameters)


Example:

A class (new object type) named Circle is created in the example below. In the main method, an object of the type Circle named MyCircle is created. The object contains just one integer attribute radius, which is accessible via the dot. operator in the main function.

public class Circle {
  //class attribute
  int radius = 10;

  public static void main(String[] args) {
    Circle MyCircle = new Circle();

    System.out.println(MyCircle.radius);
  }
}


The output of the above code will be:

10

Class Methods

The method of the class must be declared within the class declaration. It’s defined in the same way that a regular Java method is.

Example: Class method defined inside the class

A class method named area is defined within the Circle class in the example below. A class method can be accessed in the same way as a class attribute can, by using the dot. operator. The circle object’s area is returned using the area() class function.

public class Circle {
  //class attribute
  int radius = 10;
  //class method
  public double area() {
    return 22/7.0*radius*radius;
  }

  public static void main(String[] args) {
    Circle MyCircle = new Circle();

    System.out.println(MyCircle.area());
  }
}


The output of the above code will be:

314.3

 

To open the online compiler click here.

Also Read: Java – Constructors

Leave a Reply

Your email address will not be published. Required fields are marked *