Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is Upcasting and Downcasting in Java? Simple Explanation

By Rohan Vats

Updated on Nov 17, 2022 | 9 min read

Share:

Upcasting and Downcasting in Java 

You must have heard of the programming language, Java if you have even the slightest interest in programming. Its versatility and compatibility have made it a highly valued programming language. Some of the most popular applications and platforms like Google, Android OS, Netflix, Spotify, Amazon, and Minecraft use Java to support their products. 

Today, Java is one of the most popular and widely used programming languages in the world which has stood the test of time. Whether you are developing mobile applications, web applications, client-server applications, enterprise applications, or computer games, knowledge of Java is absolutely essential for programmers. 

Given how important Java is for programming, you must be itching to explore some of the most important concepts related to it. In this article, we delve into one such aspect: what is upcasting and downcasting in Java.

Before we begin exploring the concepts of upcasting and downcasting in Java, you need to understand the concept of typecasting.

Check out our free courses to get an edge over the competition.  

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.  

Check out upGrad’s Advanced Certification in Cyber Security 

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.

Check out upGrad’s Advanced Certification in Cloud Computing 

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

Let’s look more closely at what is upcasting in Java.

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 

Now that we know what is upcasting in Java, in this section, we dive deeper to explore what is downcasting in Java.

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.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

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. 
  • Upcasting is always safe, with zero chances of failure. However, downcasting has risks of failing as it can throw up a ClassCastException. So what’s the fix? The instanceof operator must be used to downcast safely. 

Now that you are geared with the knowledge of what is upcasting and downcasting in Java, you must be wondering, “Why is it important?”

Simply put, it gives you the tools to use a simple syntax to build complicated programs. Even though upcasting is rarely used in Java, if you want to generalize a function or property of the parent class to its subtype, you need to use typecasting. On the other hand, downcasting is used more frequently. Downcasting in Java is essential when depending on the subtype, you want to access specific subtype behaviours. 

It is, nonetheless, important to keep in mind that when casting an object, we are only changing the reference type of the object and not the actual object type itself. 

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.

Frequently Asked Questions (FAQs)

1. What is Java, and how has it come into existence?

2. What does typecasting mean in terms of Java?

3. Why is downcasting important?

Rohan Vats

421 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

RecommendedPrograms

SuggestedBlogs