Learn Java Tutorial for Beginners, Part 33: Upcasting and Downcasting

Pubblicato il: 08 agosto 2020
sul canale di: Quordnet Academy
655
6

In this video by Quordnet Academy for the series Learn Java Tutorial for Beginners how to use Upcasting and Downcasting have been discussed.
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :
   • Java Tutorial For Beginners  

Upcasting Vs Downcasting in Java

Typecasting is one of the most important concepts which basically deals with the conversion of one data type to another datatype implicitly or explicitly. In this article, the concept of the typecasting for objects is discussed.

Just like the datatypes, the objects can also be typecasted. However, in objects, there are only two types of objects (i.e.) parent object and child object. Therefore, typecasting of objects basically mean that one type of object (i.e.) child or parent to another. There are two types of typecasting. They are:

Upcasting: Upcasting is the typecasting of a child object to a parent object. Upcasting can be done implicitly. Upcasting gives us the flexibility to access the parent class members but it is not possible to access all the child class members using this feature. Instead of all the members, we can access some specified members of the child class. For instance, we can access the overridden methods.
Downcasting: Similarly, downcasting means the typecasting of a parent object to a child object. Downcasting cannot be implicitly.
The following image illustrates the concept of upcasting and downcasting:

Upcasting-Vs-Downcasting

Let’s understand the following code to understand the difference:

// Java program to demonstrate
// Upcasting Vs Downcasting

// Parent class
class Parent {
String name;

// A method which prints the
// signature of the parent class
void method()
{
System.out.println("Method from Parent");
}
}

// Child class
class Child extends Parent {
int id;

// Overriding the parent method
// to print the signature of the
// child class
@Override
void method()
{
System.out.println("Method from Child");
}
}

// Demo class to see the difference
// between upcasting and downcasting
public class GFG {

// Driver code
public static void main(String[] args)
{
// Upcasting
Parent p = new Child();
p.name = "GeeksforGeeks";

// This parameter is not accessible
// p.id = 1;
System.out.println(p.name);
p.method();

// Trying to Downcasting Implicitly
// Child c = new Parent(); compile time error

// Downcasting Explicitly
Child c = (Child)p;

c.id = 1;
System.out.println(c.name);
System.out.println(c.id);
c.method();
}
}
Output:
GeeksforGeeks
Method from Child
GeeksforGeeks
1
Method from Child
An illustrative figure of the above program:

Upcasting-Vs-Downcasting1

From the above example we can observe the following points:

Syntax of Upcasting:
Parent p = new Child();
Upcasting will be done internally and due to upcasting the object is allowed to access only parent class members and child class specified members (overridden methods, etc.) but not all members.
// This variable is not
// accessible
p.id = 1;
Syntax of Downcasting:
Child c = (Child)p;
Downcasting has to be done externally and due to downcasting a child object can acquire the properties of the parent object.
c.name = p.name;
i.e., c.name = "GeeksforGeeks"

Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
  / quordnet_academy  
2.Follow us on LINKEDIN for interesting content on different aspects
  / quordnet-academy  
3.Don't forget to like our FACEBOOK to get the most out of it
  / quordnetacademy  
4.Follow us on twitter to get a mix of all
  / quordnetacademy  
5.If you want to get us on TUMBLR please then click on the link given below
https://www.tumblr.com/blog/quordnet-...
6.Do join our OFFICIAL Telegram for notes of different things
https://t.me/quordnet
7.For get job update regularly both private and government do join this telegram channel
https://t.me/quordnetforjobs
#quordnetacademy, #java_tutorial_series, #javatutorialseries


In questa pagina del sito puoi guardare il video online Learn Java Tutorial for Beginners, Part 33: Upcasting and Downcasting della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Quordnet Academy 08 agosto 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 655 volte e gli è piaciuto 6 spettatori. Buona visione!