Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
19 views
asked by Geek (15.3k points) 19 43 70

1 Answer

0 votes
With objects, you can cast an instance of a subclass to its parent class.
Casting an object to a parent class is called upcasting.
Child child = new Child ();
Parent parent = child;
To upcast a Child object, all you need to do is assign the object to a reference variable of type Parent. The parent reference variable cannot access the members that are only available in Child.
 
Because parent references an object of type Child, you can cast it back to Child. It is called downcasting because you are casting an object to a class down the inheritance hierarchy. Downcasting requires that you write the child type in brackets. For example:
 
Child child2 = (Child) parent;
 
answered by Expert (15k points) 15 23 32

Related questions

0 votes
1 answer 34 views
34 views asked by smith Geek (15.3k points) 19 43 70
0 votes
1 answer 20 views
20 views asked by taye Sr Member (5k points) 7 10 12
0 votes
1 answer 41 views
41 views asked by john Guru (27.1k points) 35 53 115
0 votes
2 answers 62 views
62 views asked by john Guru (27.1k points) 35 53 115
0 votes
1 answer 19 views
19 views asked by rock Expert (15k points) 15 23 32
...