Upcasting permits an object of a subclass type to be treated as an object of any superclass type.
Questions tagged [upcasting]
244 questions
1
vote
1 answer
Upcasting std::shared_ptr to std::shared_ptr with templates
I have 4 classes in an inheritance chain: A->B->C, A->B->D, where B is the only class template.
I would like to have an std::map that maps between ids and object pointers (C or D), but I'm having trouble assigning the make_shared output to an entry…

dvilela
- 1,200
- 12
- 29
1
vote
3 answers
How can I access gets and sets in a sub class?
In this application we have the Automovel class:
public class Automovel {
private String marca;
private String matricula;
private String anoConstrucao;
private Motor motor;
private int preco = 0;
}
(with their builders, getters…

Ruben Amorim
- 15
- 6
1
vote
1 answer
When is a `static_cast (static_cast(derived))` from a pointer to a derived class valid?
For this question, no polymorphism shall be involved, i.e. no virtual methods, no virtual base classes. Just in case it matters, my case does not involve any of those.
Assume I have a class Derived which has an unambiguous accessible parent of type…

burnpanck
- 1,955
- 1
- 12
- 36
1
vote
1 answer
Difference between explicit downcast and upcast
If I have the following situation where I define a class Animal and another class Dog that extends Animal with the following two lines of code:
1) Dog d = (Dog) new Animal(); //explicit downcast
2) Animal a = new Dog(); //automatic upcast
What are…

lemonuzzi
- 21
- 1
- 3
1
vote
2 answers
How to upcast using Generic Type Class to Formal Implementation?
When we try to upcast from a Generic Type class to a formal implementation it gives a casting error.
In the code below you can see that I have a FormalClass that is an implementation of a GenericTypeClass. When I try to up cast from the…

Diogo Luis
- 228
- 2
- 9
1
vote
2 answers
upcasting and downcasting in C++
I was trying the idea of casting in C++ using Visual Studio C++ 2010 Express and the use of dynamic_cast.
But somehow, when I run it, an cat object can actually perform dog behaviour.
Seem like Dog d = (Dog)aa; got the compiler confused. Any…

aDeveloper
- 11
- 2
1
vote
6 answers
confusion about upcasting vs dynamic binding
So I am having some confusion understanding dynamic binding vs upcasting.
My original understanding was that when you create a base_class reference and assign it to a derived class object like:
base_class obj = new derived_class();
you can now…

Luc Aux
- 149
- 10
1
vote
2 answers
Java - Upcasting and Downcasting
I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that.
Upcasting - Conversion from child to parent -…

Aishu
- 1,310
- 6
- 28
- 52
1
vote
1 answer
Why do we need Upcasting & Downcasting in C#?
Consider we are having three classes, Shape which is the base class and two other classes Circle and Text that inherits from the base classe (Shape)
Shape.cs
namespace ObjectOriented
{
public class Shape
{
public int Height { get;…

D.AhmedRafik
- 81
- 2
- 15
1
vote
1 answer
What is the practical application of an assignment involving a wildcard?
what is the reason of an assignment like this?
List extends Fruit> flist = new ArrayList();
// flist.add(new Apple());
// flist.add(new Fruit());
// flist.add(new Object());
Once we "upcast" the Apple container in a Fruit container, we…

acejazz
- 789
- 1
- 7
- 27
1
vote
2 answers
Class subtyping in Rust
C++ allows class subtyping, which is really convenient because you can use functions implemented for the base class with the derived class. Rust doesn't appear to have anything like that. The feature seems to have been available at some point, but…

eugene2k
- 121
- 7
1
vote
2 answers
What is the order of Print Out about NEW Keyword in Inheritance?
It's actually part of one interview question I got confused.
class A
{
public A()
{
System.out.println("A") ;
}
}
class B extends A
{
public B()
{
System.out.println("B") ;
}
}
A a1 = new B();
…

James Xia
- 173
- 1
- 2
- 9
1
vote
1 answer
How do I specify a delegate's return type without upcasting?
I have this class:
class MyClass
{
public int Id { get; set; }
}
I can create this concrete method with a return type of object and return the integer Id without upcasting:
object ConcreteGetId(MyClass mc)
{
return mc.Id;
}
However, when I…

oscilatingcretin
- 10,457
- 39
- 119
- 206
1
vote
1 answer
How to refer to a method defined in a derived class only, using an upcast unique_ptr pointer in C++?
Assume following classes
class Base
{
void Doajob(a,b){...}
}
class Derived: public Base
{
void Doanotherjob(a,b,c){...}
}
I have defined a pointer as follows:
auto ptr= unique_ptr (new Derived(name));
Now I want to access…

peyman
- 21
- 4
1
vote
1 answer
Can I pass a subclass instance to a method which accepts superclass instance
I have a setup similar to this. When I try to call execRequest method using java Reflections and in parameterArray pass the subclass instance which is DelRequest I am getting an NoSuchMethodFound Exception
DelRequest delReq=new…

Shahbaz
- 141
- 2
- 7