×

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.

Related Topics

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.