Java Class and Object to Learn OOPs in Java


1. Objective

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, before we start with java classes and objects, let us revise our java basics.

classes and objects in java

What are classes and objects in java

2. What is Java Class and Object

Java is an Object-oriented programming language, has basic concepts are Java object and class. 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 be specified and variables can be declared. These variables in Java are known as ‘instances’ of classes which actually are the ‘objects’. The components of class declaration are-

a. Java Class Modifiers

The access of a java class can be defined, for example, it can be 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 be capitalized.

c. Java Superclass

The name of the parent of the java class (superclass), it should be preceded 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 behavior 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 behavior 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 an object is-

Dog

Objects in Java classes and objects

Java classes and objects- Objects

5. Instantiating a Class in Java

Instantiating a java class can also be 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 behavior of the class but the state for every object is different.

A class can have any number of objects or instances, for example

Instantiating a object and 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. Before instantiating a class, lets discuss the java syntax and Java Data types to help in smooth java programming.

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

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 an 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 is used 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.

Example-

Test example = new Test();
example = new Test();

  • A parent class reference can be used 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 are initiated but never stored in java reference variables. We use them for immediate calling and destroy them after use, i.e. method calling in java. They are widely used 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.

7. Conclusion

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. In the upcoming tutorials, we are going to learn more about more about the java methods and how to use them.

Refer List of Best Java Book to learn more

For reference