Upcasting and DownCasting in Java
Upcasting and Downcasting in Java is also known as generalization and specialization In Java. In Upcasting and Downcasting one type(class) of object is converted into another type(class). It’s similar to the typecasting in primitive data types in C, CPP-like languages.
Upcasting in Java:
Converting child class reference to parent class is known as Upcasting. It is by default done by the compiler. When we create an object of a child class with reference of parent class is known as Upcasting. Here we have not used any casting operator as it’s done by the compiler explicitly.
Ex:
Parent p1= new Child();
In Upcasting we can access all variables and methods of parent class and only overridden methods of the child class.
class Employee
{
public void displayEmp()
{
System.out.println(“This is Employee class”);
}
}
Class Manager extends Employee
{
@Override
public void displayEmp()
{
System.out.println(“This is Manager class”);
}
}
public Class TestEmployee
{
Public static void main(String args[])
{
Employee e1=new Manager()// upcasting
e1.displayEmp();
}
Output:
This is Manager class
For Free, Demo classes Call: 020-71173125
Registration Link: Java Classes in Pune!
Downcasting in Java:
Converting the reference of parent type in child type is known as downcasting. In the above example of upcasting we can access all public members of the parent class and only overridden methods of child class. But what if we want to access child-specific attributes and methods, will get an error that
with parent reference we can access only parent specific attributes and methods.
To overcome this problem we have to use downcasting, we can use upcasting or generalization for
accessing overridden methods of child classes, but while using generalization we can access the child-specific data by only downcasting the reference of parent into child class.
Syntax: Child c1=(Child) p1;
Class Employee
{
public void displayEmp()
{
System.out.println(“This is Employee class”);
}
}
Class Manager extends Employee
{
@Override
public void displayEmp()
{
System.out.println(“This is Manager class”);
}
//instance method of Manager
public void displayManager()
{
System.out.println(“This is Manager class specific method”);
}
Class TestEmployee
{
Public static void main(String args[])
{
Employee e1=new Manager()// upcasting
e1.displayEmp();
Manager m1=(Employee)e1; // downcasting
m1.displayManager();
}
Output:
This is the Manager class
This is Manager class-specific method
Usage:
1.In Java, we use Upcasting. We use it when we need to develop a code that deals with only the parent class.
2. Downcasting is used when we need to develop a code that accesses behaviors of the child class.
3. We can create a generalized method that accepts parent class reference as an argument, instead of providing separate references of each child class.
4. Ex we can create a method like public Employee getEmp(Employee e) which can accept the
object of any child class as an actual argument.
Ex: Employee m1=new Manager();
getEmp(m1);// here we are passing the object of Manager to getEmp method
For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!
Advantages:
- We can create generalized method.
- Code will be reduced.
- Gives the flexibility to the programmers.
Drawbacks:
- Upcasting is safe done implicitly
- Downcasting can be risky because a parent class object may not have all the features of the derived class.
- It may cause class cast exceptions.
Conclusion:
Upcasting and Downcasting help to create a generalized method which gives the flexibility to the programmer to access different child class data from the same method and access the overridden method. This can reduce the code also. Do read our Top Java Interview Questions and Answers.
Do watch the video on Java: Click Here
Author:-
Shital Chauhan
Call the Trainer and Book your free demo class for JAVA now!!!
© Copyright 2020 | SevenMentor Pvt Ltd.