×

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

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

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.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.