Inheritance and Object Casting in Java

Bindu Shrestha
3 min readJan 28, 2022

--

Inheritance is an important concept of OOP. It allows inheriting fields and methods using ‘extends’ keyword. With inheritance, we can create a class with basic features and behaviors and create its specialized versions, by creating classes, that inherit this base class. The base class is also called superclass or parent class and the class that inherits from the base class is called subclass or child class. As we can see, inheritance facilitates the reusability of code by allowing the use of existing fields and methods.

For example, we can have an Employee class that has fields like name and employeeId as we can safely assume all employees have these. We can then go ahead and create class Programmer that have a specialized field like technology, as seen in the example below.

Base class Employee
Programmer, subclass of Employee

Note: printEmployeeDesc() in Programmer overrides method printEmployeeDesc in superclass Employee.

IS-A relationship

We use inheritance when there’s is-a relationship, in our example, we used inheritance since we know that a programmer is an employee. As every class directly or indirectly inherits from Object, it is also an Object. Therefore in the example above, a Programmer is an Employee and an Object.

Casting shows the use of an object of one type in place of another. Since the reference variable only refers to an object but doesn’t contain the object itself, casting does not change the actual object but only labels an object in another way, expanding or narrowing opportunities to work with it. Upcasting narrows the list of methods and properties available to the object, and downcasting can extend it.

Upcasting:

Object obj = new Programmer(“Irene”,”1002",”Php”);

Employee emp = new Programmer(“Ian”,”1003",”Php”);

Casting from a subclass to a superclass is called upcasting. Typically, the upcasting is implicitly performed by the compiler. As can be seen from the example, in upcasting a reference variable is assigned to a more specific type. Programmer has access to the members of Employee and Object so upcasting is usually safe.

Downcasting:

Programmer programmer = (Programmer) obj;

Programmer programmer = (Programmer) emp;

Casting from a superclass to a subclass is called downcasting. Unlike upcasting, downcasting can fail if the actual object type is not the target object type. For example:

Employee emp = new Employee(“Sita”,”1005");

Programmer programmer = (Programmer) emp;

This will throw a ClassCastException during run-time because the actual object type of emp is Employee and an Employee is not a Programmer.

Example of object casting:

Continuing with our example we might want to have a method to print descriptions of employees. We could then use this method to print descriptions of different employees like Programmer or BA, and it would print descriptions specific to type.

Print description of specific type of employee

Points to be noted from the example:

  1. When calling printEmpDesc(programmer) from main method, Programmer is implicitly cast to Employee.
  2. The method that actually gets executed during runtime is of Programmer as the actual object is of type Programmer. So the output will be as seen below:
Output on console

--

--