What is Java Class and Object – Learn OOPs in Java

1. Java Class and Object – Objective

In our last session, we discussed Java Data Types. Here, in this tutorial for Java class and object, we are going to learn about the basic concepts of object-oriented programming in Java, i.e. objects and classes in java. We will learn about declaring java objects, initializing java objects, how to create multiple objects in any class in Java.

So, let’s start Java Class and Object Tutorial.

Java Class and Object to Learn OOPs in Java

Java Class and Object to Learn OOPs in Java

So, before we start with java class and object, let us revise our java basics.

2. What is Java Class and Object?

Java is an Object-oriented programming language, has basic concepts are Java class and object. It revolves around real-life entities. To know more about java, revise features of java.

Java Classes are user-defined data type from which the objects are created.

Java Objects are the basic entity, they are linked by classes and methods.

3. Java Class

A class in java is a user-defined prototype in which a data type can specify and variables can declare. These variables in Java are known as ‘instances’ of classes which actually are the ‘objects’. The components of the Java class declaration are-

a. Java Class Modifiers

The access of a java class can define, for example, it can public or can a default Java access modifiers.

b. Java Class Name

The class name in java should begin with an initial letter, which by convention should capitalize.

c. Java Superclass

The name of the parent of the java class (superclass), it should precede by the keyword. A class in Java can only extend to one parent.

d. Java Interface Class

Interfaces are implemented by the java class, they are separated by commas and are implemented by their keywords.

e. Java Body Class

The body of a java class is in the curly braces {}.

There are various types of classes in java like nested classes, lambda classes, and anonymous classes. Constructors are used to initializing any object in the class. The behaviour of the class is defined by the java methods.

4. Java Object

A Java program can have as many objects as it requires, objects in Java interact through methods. Java objects correspond to real life entities, an object consists of-

a. Java Object State

A state represents the properties of an object and is defined by the attributes if the object.

b. Java Object Behavior

A behaviour of an object represents the response of an object with other objects, it is defined by the method.

c. Java Object Identity

An identity enables one object to interact with another object, it gives a unique name to the Java object.

Example of Java object is-
Dog

Java Class and Object to Learn OOPs in Java

Objects in Java classes and objects

5. Instantiating a Class in Java

Instantiating a java class can also called a declaration of a Java object. When an object is created, it is known as instantiating of class. All the instances share common attributes and behaviour of the class but the state for every object is different.

Do you know What is Polymorphism in Java?

A class can have any number of objects or instances. For example

java class and object- Instantiating a class in Java

, java class and object- Instantiating a class in Java

Here, the name of the java class is a dog, the various java objects are Dog 1, Dog 2, Dog 3 and Dog 4 who share the common attributes and behavior.

a. Initializing an Object in Java

Initializing is done by a java constructor, a memory is allocated and a reference is given to that memory.

// Class Declaration
public class Dog
   {
// Instance Variables
      String name;
      String breed;
      int age;
      String color;
// Constructor Declaration of Class
         public Dog(String name, String breed,
         int age, String color)
            {
                this.name = name;
                this.breed = breed;
                this.age = age;
                this.color = color;
            }
// method 1
      public String getName()
    {
        return name;
     }
// method 2
      public String getBreed()
    {
        return breed;
    }
// method 3
      public int getAge()
    {
        return age;
    }
// method 4
      public String getColor()
       {
            return color;
       }
@Override
      public String toString()
       {
            return("Hi my name is "+ this.getName()+
            ".\nMy breed,age and color are " +
            this.getBreed()+"," + this.getAge()+
            ","+ this.getColor());
      }
public static void main(String[] args)
      {
            Dog tuffy = new Dog("shadow","german shephard", 2, "golden");
            System.out.println(shadow.toString());
      }
 }

Output-

Hi my name shadow.
My breed,age and color are german shephard,2,golden

Is Java An Object-Oriented Language or Not?

b. Ways to Create Object of a Class in Java

There are basically 4 ways to create an object in Java, they are-

i. Using a new keyword

It is the most common method to create a Java object.

Example-

// creating object of class example
Example t = new Example();

ii. Using Class.forName(String className)

This method is a pre-defined class in java.lang with the name class.this method returns the Class object with string name associated with it.

Example-

Example obj = (Example)Class.forName("com.p1.Test").newInstance();

iii. Using Clone method in Java

This method returns the copy of the object.

Example-

// creating object of class Test
Test t1 = new Test();
// creating clone of above object
Test t2 = (Test)t1.clone();

iv. Deserialization

This technique use to read an object from a saved state of file.

Example-

FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
Object obj = in.readObject();

c. Creating Multiple Objects by One Type only

  • We need different objects in different methods of a class, and saving different variables for them is not a good practice so we declare a static variable, this saves a lot of memory.

Do you know How to Create Object in Java?

Example-

Test example = new Test();
example = new Test();
  • A parent class reference can use to declare or store a subclass object, this helps in switching into different subclass objects using same reference variable.

Example-

class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public class Test
  {
// using Dog object
Animal obj = new Dog();
// using Cat object
obj = new Cat();
  }

6. Anonymous Objects in Java

Anonymous objects are the Java objects that initiate but never store in java reference variables. We use them for immediate calling and destroy them after use, i.e. method calling in java. Java objects widely use in many different libraries, for example in AWT libraries.

Example-

btn.setOnAction(new EventHandler()
    {
        public void handle(ActionEvent event)
            {
                System.out.println("Hello World!");
            }
    };

Here btn is the anonymous object to call EventHandler in java.

Let’s revise Java Operators.

So, this was all about Java Class and Object. Hope you like our explanation

Conclusion

Hence, in this java class and object tutorial, we learned the basics of Classes and Objects in Java, we learned how to declare java objects and its various types. Furthermore, if you have any query, feel free to ask in the comment box.

Refer List of Best Java Book to learn more

For reference

Leave a Reply

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