Calling Object in Java

In Java, object calling can be considered as an important concept related to object-oriented programming (OOP). The process of object calling gets started by the instantiation of a class, which is utilized for representation of a blueprint that can be later utilized for the creation of objects. Let's understand about the process of object calling in Java in detail.

In Java, the new keyword is mainly utilized for creation of an object of a class, invoking the respective constructor and allocating memory along with initialization of object. The object of a particular class is created by using the new keyword which is followed by the class name. The usage of new keyword is mandatory as it used for allocation memory, initializing object of the respective class. An instance of the respective class is created when new keyword is utilized in Java and this instance gets assigned to the reference variable which will be identified as the object name.

Upon successful creation of the object of the class, it can be interacted by invoking the methods of the class using dot operator. Along with methods of the class, other fields can also be accessed by the objects such as variables.

Let's understand about the process of object calling in Java in detail with the help of few example programs in Java.

Example 1:

Filename: ObjectClass.java

Output:

BaseNumber: 10
BaseNumber: 20
DerivedMessage: Hello
New BaseNumber: 15
Interface method implementation in ClassDerived
Messages:
Message 1
Message 2
Abstract method implementation in ChildClass
Non-abstract method in ObjectCalling Class

Let's understand with the help of another example Java program.

Example 2:

Filename: ObjectCalling.java

Output:

Animal_Class{name='Animal Class'}
Generic animal sound
Mammal Animal{name='Dog', numberOfLegs=4}
Mammal sound
Bird_Animal{name='Sparrow', canFly=true}
Bird Animal sound
Polymorphic behavior in a list:
Animal_Class{name='Animal Class'}
Generic animal sound
Mammal Animal{name='Dog', numberOfLegs=4}
Mammal sound
Bird_Animal{name='Sparrow', canFly=true}
Bird Animal sound





Latest Courses