Upcasting & Downcasting in Java with Examples | Full Tutorial

Devstringx Technologies
5 min readMar 15, 2023

--

Java is an object-oriented programming language that allows developers to create classes and objects. Upcasting and downcasting are two concepts that are related to class inheritance and object polymorphism.

Upcasting In Java and How Does It work?

It is a process of converting a subclass object to its superclass reference type. In other words, it is a way of treating an object of a subclass as an object of its superclass. This conversion is done implicitly by the Java Virtual Machine (JVM) at runtime.

When upcasting is performed, the subclass object loses its specific attributes and methods that are not present in its superclass. The resulting object can only access the methods and attributes that are defined in the superclass.

Upcasting is useful when you want to use a single reference type to refer to objects of different subclasses. This allows you to write more generic code that can handle different types of objects without having to write separate code for each type.

Example of Upcasting in Java

Let’s consider an example of upcasting in Java. Suppose we have a class hierarchy that looks like this:

class Animal 
{
public void makeSound()
{ System.out.println("Animal is making a sound"); }
}
class Dog extends Animal
{
public void makeSound()
{ System.out.println("Dog is barking");
}
public void fetch()
{
System.out.println("Dog is fetching");
}
}
Dog myDog = new Dog(); Animal myAnimal = myDog;

In this code, we create a Dog object called myDog. We then upcast myDog to an Animal reference called myAnimal. Now, myAnimal can only access the methods and attributes that are defined in the Animal class:

myAnimal.makeSound(); // prints “Dog is barking”

Good to Read:- Easiest Way to Learn What Is Garbage Collection In Java

Downcasting in Java and How Does It Work?

Downcasting is the opposite of upcasting. It is a process of converting a superclass reference type to its subclass object. In other words, it is a way of treating an object of a superclass as an object of its subclass. This conversion is done explicitly by the programmer using the cast operator.

When downcasting is performed, the superclass reference is checked to see if it refers to an object of the subclass. If it does, the reference is converted to the subclass type. If it doesn’t, a ClassCastException is thrown at runtime.

Downcasting is useful when you want to access the specific methods and attributes of a subclass object that are not present in its superclass. However, it should be used with caution because it can lead to runtime errors if the superclass reference does not refer to an object of the subclass.

Example of Downcasting in Java

Let’s consider an example of downcasting in Java. Suppose we have the same class hierarchy as before:

class Animal

{

public void makeSound()

{

System.out.println("Animal is making a sound");

}

}

class Dog extends Animal

{

public void makeSound()

{ System.out.println("Dog is barking");

}

public void fetch() {

System.out.println("Dog is fetching");

}

}

Now, let’s create an Animal object and downcast it to a Dog reference:

Animal myAnimal = new Dog(); Dog myDog = (Dog) myAnimal;

In this code, we create an Animal object called myAnimal. We then downcast myAnimal to a Dog reference called myDog. Now, myDog can access the specific methods and attributes that are defined in the Dog class:

myDog.makeSound(); // prints “Dog is barking” myDog.fetch(); // prints “Dog is fetching”

Difference Between Upcasting and Downcasting

The main difference between upcasting and downcasting is the direction of the conversion. Upcasting converts a subclass object to its superclass reference, while downcasting converts a superclass reference to its subclass object.

Upcasting is done implicitly by the JVM at runtime, while downcasting is done explicitly by the programmer using the cast operator.

It is safe because it only allows access to the methods and attributes that are defined in the superclass. Downcasting, on the other hand, can be dangerous because it can lead to runtime errors if the superclass reference does not refer to an object of the subclass.

Good to Read:- Top Features of JavaScript You Must Know

When to Use Upcasting and Downcasting in Java?

Upcasting is useful when you want to write generic code that can handle different types of objects without having to write separate code for each type. It is also useful when you want to create collections of objects that have a common superclass.

Downcasting is useful when you want to access the specific methods and attributes of a subclass object that are not present in its superclass. However, it should be used with caution because it can lead to runtime errors if the superclass reference does not refer to an object of the subclass.

Best Practices for Upcasting and Downcasting in Java

Here are some best practices for using upcasting and downcasting in Java:

  • Use upcasting to write generic code that can handle different types of objects without having to write separate code for each type.
  • Use downcasting sparingly and only when you need to access the specific methods and attributes of a subclass object that are not present in its superclass.
  • Always check the type of the object before downcasting to avoid runtime errors.
  • Avoid using downcasting in performance-critical code because it can be slow.

Common Mistakes to Avoid When Using Upcasting and Downcasting in Java

Here are some common mistakes to avoid when using upcasting and downcasting in Java:

  • Using downcasting without checking the type of the object first can lead to runtime errors.
  • It excessively can make the code difficult to read and maintain.
  • Using upcasting with complex object hierarchies can result in unexpected behavior.
  • Forgetting to cast the object before downcasting can also lead to runtime errors.

Good to Read:- Difference Between C++ and Java | C++ Vs Java

Conclusion

In conclusion, upcasting and downcasting are two important concepts in Java that allow developers to work with class inheritance and object polymorphism. Upcasting is the process of converting a subclass object to its superclass reference, while downcasting is the process of converting a superclass reference to its subclass object.

Upcasting is useful for writing generic code that can handle different types of objects, while downcasting is useful for accessing the specific methods and attributes of a subclass object that are not present in its superclass. However, downcasting should be used with caution because it can lead to runtime errors.

A skilled Java developer can bring your ideas to life and ensure that your software runs smoothly and seamlessly. Don’t wait any longer to take your business to the next level — hire a Java developer now and start building the applications that will help you succeed!

Originally published at https://www.devstringx.com on March 15, 2023

--

--

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services