Upcasting in Java
To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java.
Casting
The process of providing or replacing a reference variable of one type with an object of another type is known as casting in Java.
Simply, the conversion of a data type into another data type is termed as casting or type casting in Java. With datatypes, the object may also be considered for type casting. There are different kinds of objects. One is parent object and the other is child object.
In Java, there possibly two types of type casting considered for objects. They are child object to parent object and parent object to child object. This can also be termed as downcasting and Upcasting respectively.
In this article, we are going to acknowledge ourselves about upcasting, how it works and also a simple program.
Upcasting
The typecasting of child object into a parent object is technically called as upcasting. It can be performed in implicit manner. Upcasting allows us the ability to acquire the members of the parent class, although it is not able to use this ability to access all the members of the child class. We can obtain a part of the child class's members rather than all of them. As an example, we can use the overridden methods.
Generalization and Widening are the other terms to refer the upcasting technique in Java.
Example(1)
File name : Demo111.Java
// Java program to describe the Upcasting technique
class Father // Parent Class
{
String name;
void description()
{
System.out.println("Parent");
}
}
class Son extends Father // A Child class which extends the parent class.
{
String color;
void description()
{
System.out.println("Child");
}
}
public class Demo111
{
public static void main(String[] args)
{
Father n = new Son();
n.name = "Mikael";
Son e = new Son();
e.name = "Elijah";
System.out.println("Object n");
System.out.println("Name: " + n.name);
a.description();
System.out.println("Object e");
System.out.println("Name: " + e.name);
e.description();
}
}
Output
Object n
Name: Mikael
Child
Object e
Name: Elijah
Child
File name : Demo123.Java
class Mother
{
void DisplayData()
{
System.out.println("method that belongs to parent class");
}
}
class Daughter extends Mother
{
void DisplayData() {
System.out.println("method that belongs to child class");
}
}
class Demo123 {
public static void main(String args[])
{
Mother d1 = new Daughter();
Mother d2 = new Daughter();
d1.DisplayData();
d2.DisplayData();
}
Output:
method that belongs to child class
method that belongs to child class
It is noticeable that while being the child type, we cannot access child class members that used a parent class references.
We also notice that by having the same parent class reference object, we can retrieve both the members of the parent class and the overridden methods of the child class.
Why is Upcasting Necessary
If you've got overloaded methods and you do not wish to call the specialized one, upcasting may be required. Upcasting is useful in Java when you wish to ensure the application of a certain method overloading.
Summary
- A parent object obtains a typecast of a child object.
- Upcasting can be done both explicitly and implicitly.
- We have accessibility to the parent class's variables and methods in the child class.
- We have accessibility to a few specific child class methods.