Upcasting & Downcasting (Part 6)
Pre-requisite : Inheritance , Methods
Upcasting & Downcasting
Typecasting is one of the most important concepts which deals with the conversion of one data type to another datatype implicitly or explicitly. In this blog , the concept of the typecasting for objects is explained.
Just like casting of datatypes, objects can also be typecasted. However, in objects, there are only two types of objects parent object and child object. There are two types of typecasting :
1. Up-Casting
Upcasting is 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 , overridden methods can be accessed.
Note:-
As we saw in Inheritance , A Parent Class can have many child classes i.e many child classes can inherit a parent class . It means that a child can inherit all the properties of a parent ( Up Casting ) however a parent may or may not inherit child's properties but in Java we can forcefully cast a child to a parent ( Down Casting ) , the compiler checks the background of the casting that it is possible or not. If its not possible the compiler throws ClassCast exception i.e an error.
We will learn about exceptions in later tutorials , for now we can understand it as an error.
Syntax of up-casting:-
Child_Class
[obj name]
= new Parent_Class( );
eg:-
class
Parent
{ public String
pName
; void
getInfo
(String
pName
) {
this
.
pName
=
pName
; System.out.println("
Showing name of Parent:
" +
pName
); } } class
Child extends Parent
{ public String
cName
; void
getInfo
(String
cName
) {
this
.
cName
=
cName
; System.out.println("
Showing name of Child:
" +
cName
); } } public class
MyClass
{ public static void main(String[] args) {
// Upcasting
Parent
p
= new Child();
// able to access parent name
p.pName
= "
Bob
";
p
.
getInfo
("
Bob
");
//prints child getInfo() because obj p points to Child class
// unable to access child name // p.cName="John";
} }
Output:-
Showing name of Child: Bob
Here the method of parent class is overridden by the child class.So when we try to call the method of parent class , the method of child class is called i.e method of parent class hides behind method of child class.This concept is called as Method Hiding. We will learn more about this in the future tutorials.
2. Down-Casting
Down-casting means the typecasting of a parent object to a child object. Downcasting cannot be done implicitly. Either way down-casting is also a practice that should not be followed unless it is required.
Syntax of down-casting:-
Child_Class
[obj name]
= (Child_Class)
[parent obj]
Explaining down-casting with the same program , to get a better understanding of it.
class
Parent
{ public String
pName
; void
getInfo
(String
pName
) {
this
.
pName
=
pName
; System.out.println("
Showing name of Parent:
" +
pName
); } } class
Child extends Parent
{ public String
cName
; void
getInfo
(String
cName
) {
this
.
cName
=
cName
; System.out.println("
Showing name of Child:
" +
cName
); } } public class
MyClass
{ public static void main(String[] args) {
//Downcasting
Parent
p
=new Child();
//upcasted object of parent
// Child c = new Parent(); //cannot invoke implicitly
Child
c
= (Child)
p
;
//invoking explicitly
// able to access parent name
c
.
pName
="
Bob
";
c
.
getInfo
("
John
");
//prints child getInfo()
//able to access child name
c
.
cName
="
John
"; } }
Output:-
Showing name of Child: John