Last Updated on March 22, 2023 by Prepbytes
There are many programming languages available some of them support object-oriented programming whereas some of them do not. Java is one of the languages that support OOPs. Among the 4 pillars of oops inheritance is one of the most used ones which allows the child classes to inherit the methods and attributes of the parent class. Upcasting and downcasting in java are important aspects of inheritance. While moving further in this article we will learn all about upcasting and downcasting in java along with why we use them, examples of both and followed by applications of them.
Typecasting in Java
The process of converting one data type into another is known as typecasting in java. It is one of the most important concepts of java. As it deals with the conversion of one data type into another data type explicitly or implicitly.
Like the data type, we can also typecast the objects.
You can refer to Typecasting in Java for further knowledge on typecasting.
There are two types of object typecasting available in java and they are mentioned below:
- Upcasting
- Downcasting
Upcasting in Java
In simple words upcasting in java is a type of object typecasting where we are typecasting a child object to a parent object. The upcasting is done implicitly there is no need to explicitly refer to the parent class. The upcasting gives us the benefit of accessing all the methods and variables of the parent class. But in upcasting we cannot access all the variables and methods of the child class. We can only access some specified methods and variables of the child class. Upcasting can also be referred to as Widening and Generalization.
Syntax of Upcasting in Java
The syntax of upcasting in java is given below:
Parent p= new Child();
Example of Upcasting in Java
Below is the example of upcasting.
class Vehicle{ void drive(){ System.out.println("Driving a vehicle..."); } } class Car extends Vehicle{ void drive(){ System.out.println("Driving a car..."); } void speedUp(){ System.out.println("Speeding up a car..."); } } class Main{ public static void main(String[] args){ Vehicle v = new Car(); v.drive(); } }
Output
Driving a car… when a reference variable of a superclass is assigned to a reference variable of
Explanation of the above example
In the above example we have a parent class Vehicle and a child class with the name Car. The Car class extends the Vehicle class and overrides the drive() method. In the main() class we have created an object of the car class and then assign it to the reference variable of the vehicle class.
Even though we have assigned a Car object to a Vehicle reference variable, the drive() method of the Car class is invoked because the Car class overrides the drive() method of the Vehicle class.
Downcasting in Java
Another form of object typecasting is downcasting. A parent class reference object is given to the child class during downcasting. Although it is not possible to give a parent class reference object to a child class in Java, if downcasting is used, there won’t be any compile-time errors. The "ClassCastException" is thrown when we attempt to execute it. The question is, why is downcasting permitted by the compiler if it is not feasible in Java? In some Java situations, downcasting is an option. The master class in this case refers to the subclass object.
Syntax of Downcasting in Java
The syntax of downcasting in java is given below:
Child c= (Child) p;
Example of Downcasting in Java
Below is the example of downsampling in java with code implementation.
class Vehicle{ void drive(){ System.out.println("Driving a vehicle..."); } } class Car extends Vehicle{ void drive(){ System.out.println("Driving a car..."); } void speedUp(){ System.out.println("Speeding up a car..."); } } class Main{ public static void main(String[] args){ Vehicle v = new Car(); Car c = (Car) v; c.drive(); c.speedUp(); } }
Output
Driving a car...
Speeding up a car...
Explanation of the above example
In the above example we have a parent-class vehicle and a child-class car. The car extends to the reference of the parent class and overrides the drive method. It also has a new method known as speedup. We have created an object of type car in the main function then downcast it. In the above program, we upcast a Car object to a Vehicle reference variable and then downcast it back to a Car reference variable. This allows us to call the methods of the Car class.
Why do we use Upcasting and Downcasting in Java?
We do not use upcasting too often in java, We use upcasting in java only when the code needs to deal with only the parent class not much with the child class. Whereas we use downcasting when we need to access all the methods of child classes.
Difference between Upcasting and Downcasting
Here are some of the differences between Upcasting and Downcasting.
Basis | Upcasting | Downcasting |
---|---|---|
Direction | It always goes from subclass to superclass. | It goes from superclass to subclass. |
Safety | It is comparatively safe as it does not require checks on runtime. | We need to perform regular checks on runtime. |
Access | In the child class we can only access the methods of the parent class. | We can access the methods of both child and parent classes. |
Method | We can perform this implicitly or explicitly. | We can perform this only explicitly. |
Conclusion
Upcasting and Downcasting in java are very essential components as they are used in inheritance and polymorphism. Among upcasting and downcasting in java upcasting is secure and can be done implicitly while downcasting is not properly secured so it always has to do explicitly. We have to use the instance of the operator to check whether it is downcast or not. By using upcasting and downcasting in java we can write the code in a more flexible manner. We have learned all about them in this article.
Frequently Asked Questions
Below are some frequently asked questions and answers about upcasting and downcasting in java.
1. What is the instanceof operator in Java?
The instanceof operator is used to check whether an object is an instance of a particular class or subclass.
2. How is polymorphism achieved in Java?
Polymorphism is achieved in Java by using upcasting and downcasting.
3. Can method overriding be achieved without upcasting and downcasting in Java?
No, method overriding requires upcasting and downcasting in Java to be achieved.
4. How is object casting achieved in Java?
Object casting is achieved in Java by using upcasting and downcasting.
5. What is an interface in Java?
An interface in Java is a collection of abstract methods that can be implemented by any class.