Programs

What is Upcasting and Downcasting in Java? Simple Explanation

Upcasting and Downcasting in Java 

Typecasting in Java refers to the procedure that involves the conversion of one data type to another.  This can be done both automatically and manually. While a programmer has to do the manual conversion on their own, a compiler is used for the automatic process. Downcasting and upcasting essentially are two types of object typecasting.  

Child and parent objects are two distinctive types of objects. Now, there are two ways with the help of which the objects can be initialized, subsequently also inheriting the properties that the child and parent classes have. These ways include: 

  • Child c = new Child(): Accesses all members coming under child and parent classes 
  • Parent p = new Child():  Accesses the parent class members only, as well as the methods that have been overridden in the child class 

Essentially, Parent to Child type casting is known as Upcasting, while Child to Parent type casting is referred to as Downcasting.

The typecasting system is used to see to it that the variables are processed by a function accurately or not.  While downcasting is not possible implicitly, one can perform upcasting both explicitly and implicitly.

Both downcasting and upcasting are important elements of Java and enables people to develop complex programs with the usage of simple syntax.  They also have some great advantages like grouping diverse objects together or Polymorphism

Learn Online software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Upcasting

In upcasting, a child object is a typecast to a parent class object. With the usage of this process, people can avail access to the distinguished methods and variables of the parent to child class with ease. Access to only some of the particular methods and variables of the child class, such as the overridden methods, are required in the process. In many instances, upcasting is also called widening and generalising. Upcasting can be carried out implicitly, and provide people with the flexibility to access the members of the parent class. 

The following example can help in providing a better understanding of the process. 

Example: There is an animal class that features diverse classes of animals, fish being one of them. Now, assuming that the animal class is extended by the fish class, there would be two ways of inheritance. Inheritance is one of the key aspects of Object Oriented Programming or OOP which allows one class to inherit the features of another one. This implies the derived class which is a superset of the base class. There might be more methods featured here than the base class.  During the upcast, the only thing that may happen with the class interface is that it can end up losing methods rather than gaining them. As a result of this, the compiler enables upcasting without any type of special notations or explicit casts.

Composition vs Inheritance 

When it comes to object-oriented programming, there is a high chance that one may end up creating and using done by just packaging methods and data together in a class, while subsequently making use of the objects present in that class. People may even use certain existing classes to develop new classes with composition. Inheritance is used comparatively rarely. While learning OPP even though inheritance does get a lot of focus, it is important that developers know its correct usage and do not try to use it simply wherever they can. 

In fact, one must use it quite rarely, only for the scenarios where inheritance is actually found to be useful.  Among the simplest ways to identify whether a person should use inheritance or composition is to question themselves about if they need to upcast from their new cast to the base cast. In case upcasting is a necessity then one would have to use inheritance. However, if it is not a requirement, then one must further dwell on the need for inheritance.  

Downcasting 

Downcasting is another form of object type casting and is used in appointing the subclass reference object to the parent class that is not allowed in Java.  Downcast is not too difficult to understand. For instance, if there is a base class and a class that either indirectly or indirectly derives from the base class then any time an object of the base class type goes on to being typecast into a derived class, it is referred to as a downcast.  Consequently, as per downcasting, one would go down the inheritance diagram by striving to take a base class object and then converting it into a derived class type. However, it is important to note that based on the code being written,  downcasting may or may not make sense. 

As people carry out the downcasting system, they won’t come across any type of compiler error. However, they would face java.lang.ClassCastException, which is a runtime exception. So now the question arises if downcasting is not possible in Java then why do people perform it? Simply put, downcasting can be used only in particular situations where the object referred by the parent class belongs to a subclass.

Downcasting can be extremely useful when comparing one object to another. 

To check if a downcasting is valid or not, one can run 

getClass() system enables the developers to understand the class of the object being passed in, and subsequently compare it with the current class. In case they are not equal, then downcasting shall not be valid and trying out this process will be pointless.  The concept of downcasting majorly uses the casting of super-types to the subtypes. Such a casting process is usually done in an explicit manner. This basically implies that larger sized types are made to explicitly fit into small size types.

Upcasting vs Downcasting

Upcasting and. Downcasting has certain very particular differences. Some of them have been briefly underlined in the following pointers:

  • In Upcasting a child object is typecast to a parent object. On the other hand, the reference of the parent class object ends up passing on to the child class when it comes to downcasting. 
  • People can perform upcasting both explicitly and implicitly, while downcasting can only be performed explicitly.
  • In the case of upcasting, people can access the variables and methods of the parent class in the child class but not the other way round. In downcasting, however, the variables and methods of both the classes are easily accessible. 
  • Upcasting allows access to only particular child class methods. Both when performing downcasting, all the methods and variables coming under both the classes are made accessible. 

Conclusion 

Upcasting is used rarely in Java. In most cases, people use it when they have to develop a code that just deals with the parent class. When it comes to developing a code that provides access to the behaviour of the child class, downcasting tends to be used. 

If you want to learn Java and earn a certification in cloud computing as well, upGrad offers an Advanced Certificate Programme in Cloud Computing. The 7.5-month online course is specifically designed for working professionals to help them gain entry into cloud computing roles.

Want to share this article?

Become a Full Stack Developer

0 replies on “What is Upcasting and Downcasting in Java? Simple Explanation”

Our Popular Software Engineering Courses

Get Free Consultation

0 replies on “What is Upcasting and Downcasting in Java? Simple Explanation”

×
Let’s do it!
No, thanks.