java object class

Java Object Class Tutorial

In this tutorial, you will learn about everything about “Java Object Class”

Classes and objects  in Java are the two most essential Java concepts that every programmer must learn. Classes and objects are closely related and work together. An object has behaviors and states, and is an instance of class. For instance, a cat is an objectβ€”it’s color and size are states, and its meowing and clawing furniture are behaviors. A class models the object, a blueprint or template that describes the state or behavior supported by objects of that type.

What is Class In Java?

A class is a blueprint for the object. Before we create an object, we first need to define the class.

We can think of the 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 Create a Class In Java?

We can define a typical class in Java using the following syntax:

<Access Specifier> class <class name>{

//Body

public class Student{
String Name;
   int rollno;
   String section;
   void study() {
   }
   void write() {
   }
   void play() {
   }
}

Object

It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical object class Java program creates many objects, which as you know, interact by invoking methods. An object consists of : 

πŸ‘ͺState: It is represented by attributes of an object. It also reflects    the properties of an object.

πŸ‘ͺBehavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.

πŸ‘ͺIdentity: It gives a unique name to an object and enables one object to interact with other objects.

Example :

className object = new className();

// for Bicycle class

Bicycle sportsBicycle = new Bicycle();

Bicycle touringBicycle = new Bicycle();

Access Modifier

Object-oriented programming languages like Java provide the programmers with four types of access modifiers.

πŸ‘ͺPublic

πŸ‘ͺPrivate

πŸ‘ͺProtected

πŸ‘ͺDefault

These access modifiers specify the accessibility and users permissions of the methods and members of the class.

Rules for Creating Classes

The following rules are mandatory when you’re working with Java classes:

  • The keyword “class” must be used to declare a class
  • Every class name should start with an upper case character, and if you intend to include multiple words in a class name, make sure you use the camel case
  • A Java project can contain any number of default classes but should not hold more than one public class
  • You should not use special characters when naming classes
  • You can implement multiple interfaces by writing their names in front of the class, separated by commas
  • You should expect a Java class to inherit only one parent class 

How to declare a Object in Java?

There are three major steps or points to be considered :-

  • Declaration
  • Instantiation
  • Initialization

Example:

public class Student {
   public Student (String name) {
      System.out.println("parameters sent are :" + name );
   }
   public static void main(String []args) {
      Student mystudent = new Student( "Steven" );
   }
}


Output :

Steven

Java Class Object and Methods Example

In this example we will do a java class object program –

public class ClassInJava {
    String brand;
    String model;
    int modelno;
    //Constructor
    ClassInJava(String brand,String model,int modelno){
        this.brand=brand;
        this.model=model;
        this.modelno=modelno;
    }
    public static void main(String[] args) {
        //Object of class ClassInJava
        ClassInJava object=new ClassInJava("Redmi" , "Redmi Note 13" ,1010);
        System.out.println("Mobile Brand is: " + object.brand);
        System.out.println("Mobile Model is: " + object.model);
        System.out.println("Mobile's Model No. is: "+ object.modelno);   
   }
}


Output :

Mobile Brand is: Redmi
Mobile Model is: Redmi Note 13
Mobile’s Model No. is: 1010

Java Object Class is basically the parent class of all classes. In other words, it is the topmost class of java. By Default, It extends to every class of java. Object class comes under the java.lang package.

Lang package has included by default. If any Java class does not extend any other class then it is a direct child class of Object and if extends other class then it is indirectly derived. Therefore we can easily use all the methods of Object class. Object class has many inbuilt methods that we can use to fulfill the requirement. Check other Java Turorial and examples.

Object Class Declaration

public class java.lang.Object

Class Constructors

Object class has only one constructor.

Object()

Object Class Methods

Java Object class has many inbuilt methods. These methods are very commonly used in programming. But we do not know exactly which method belongs to Object Class. There are list of all methods of Object Class. In this tutorial, we will explain each and every method with example.

1.) protected Object clone()

Parameters : Not Accept Any Parameter.
Return : Return the copy of this object.

2.) public boolean equals(Object oj)

Parameters : It accepts object which we want to compare.
Return : Return the boolean value true if given object matches otherwise it returns false.

3.) protected void finalize()

Garbage Collector calls this method. When garbage collection determines that there are no more references to the object. So this method automatically call and perform operation.

Parameters : No Parameters.
Return : NA.

4.) public final Class getClass()

Parameters : No Parameters.
Return : getClass() method returns the object of the class.

5.) String toString()

Java.lang.toString() method is very important and common used in Java. Because it converts any Object to String.

Parameters: NA
Return: This method will return the String representation of the Object.

6.) void notify()

Java.lang.Object.notify() method used in the Java Multithreading. But this method comes under the java.lang package. This method wakes up a single thread that is waiting on this object’s monitor. If many threads are waiting on this object, one of them is chosen to be awakened. So we can use this method.

Parameters: NA
Return: This method does not return anything. Because it has different functionality mentioned above.

We hope you like the post. If you have any doubt please comment below. So we can improve the content.

Leave a comment

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

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!