Objects in Java

By YuwebDesign

  1. A class is a template or blueprint from which objects are created.
    Object is an instance (result) of a class,
    and may be called a class instance or class object.
  2. Object is a real-world entity.
  3. Object is a runtime entity.
  4. Object has states and behaviors.

Class name (class is the data type) followed object name (or identifier) = keyword new followed by class name with parentheses (parentheses = constructor):

Type identifier = new Type constructor();

Or to better visualize:

ClassName objectName = new ClassName();

Instantiation of an Object is also known as construction.

Constructor looks like a method but it is in fact not a method.

Objects are created from classes by subroutines called constructors,
and destroyed by destructors.

  1. Constructor name must match the class name.
  2. Constructor cannot have a return type as it does not return any value.
  3. Constructor is called when the object is created.
  4. All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.
MyClass obj = new MyClass();

With “new MyClass()” (object allocation) we are calling
the default constructor of class myClass
to create a new object (or instance).

  1. Default Constructor:
    • If a user doesn’t create a constructor implicitly (no other constructor is defined by the user) a default constructor will be created.
    • Default constructor does not take any inputs/arguments.
    • Its main purpose is to initialize the instance variables with the default values.
    • Also, it is majorly used for object creation.
  2. Parameterized Constructor:
    • Parameterized constructor can take the arguments.
    • Parameterized constructor is capable of initializing the instance variables with the provided values.

  1. Constructors that take parameters are known as parametrized constructors.
  2. Constructor parameters are used to initialize attributes.
public class ConstructorExample {
   int age;
   String name;
   //Default constructor
   //Parameterized constructor
   ConstructorExample(String n,int a){
   public static void main(String args[]){
	ConstructorExample obj1 = new ConstructorExample();
	ConstructorExample obj2 = 
		       new ConstructorExample("ForeverYoung", 99);
	System.out.println(obj1.name+" "+obj1.age);
	System.out.println(obj2.name+" "+obj2.age);


Evergreen 16
ForeverYoung 99

In OOP languages, words “object” and “instance” are used interchangeably,
because they are a representation of that class at a particular instant.

In practice, use the instance to say about one particular object,
and use an object to talk about many objects.

Object: Car
Attributes (or state or fields or values or variables): Color, Weight, Model
Methods (or behavior): Drive, Break, Accelerate, Slow Down, Gear change

Object: House
State: Address, Color, Area
Behavior: Open door, close door

class House {
   String address;
   String color;
   double area;
   void openDoor() {
   void closeDoor() {

  1. States can be represented as instance variables (properties).
  2. Every object of same class can have different instance variable values.
    E.g., every house can have a different address, color etc.

  1. Behaviors can be represented as methods of the class.
  2. Every instance of a particular class has the same methods,
    but the methods can behave differently
    based on the value of the instance variables.

Object’s behavior acts on its state – that’s the whole point of an object.

In other words, methods use instance variable values.

E.g., “if dog is less than 14 pounds, make yippy sound, else…”
“increase weight by 5”
= change state.

  1. Object identity is typically implemented via a unique ID.
  2. The value of the ID is not visible to the external user.
  3. However, it is used internally by the JVM to identify each object uniquely.

Abstraction is a process where

  1. you show only “relevant” data
  2. and “hide” unnecessary details of an object from the user.

Read more about Abstraction OOPs concept >>>

Encapsulation binding object state (fields) and behavior (methods) together.

Read more about Encapsulation OOPs concept >>>

  1. A single object by itself may not be very useful.
    An application contains many objects.
  2. One object interacts with another object
    by invoking methods on that object.
    It is also referred to as Method Invocation.

Java OOPs Concepts Message Passing

Leave a Reply or Comment

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