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
Java Comparing two properties by object references
When overriding an equals property for one of my classes is it possible to implement it as so? The properties in question such as identifier could be String, boolean, Date, Set, or LinkedHashSet
public boolean equals(Object obj)
{
if (this ==…

FooBar
- 1,663
- 2
- 12
- 19
1
vote
2 answers
Use Method by Real Type
I learned that I can use the real type of a Object to define which Method is used, such like this:
[...]
Object foo = new String("hello");
[...]
bla(foo);
void bla(String x){
}
void bla(Integer x){
}
Now it should take the bla(String x) method,…

reox
- 5,036
- 11
- 53
- 98
1
vote
3 answers
Can someone please explain this Java upcasting behavior I am little confused?
In the code given below when we are trying to initiate object of type A by calling the constructor of a child class when we print x.i java refers to the parent class attribute and why when called through a method it refers to a child class…

Ajinkya Ronghe
- 13
- 3
1
vote
2 answers
Acceptable programming practice - conversion and upcasting
Is the following acceptable programming practice:
class TestA
{
protected:
int A;
public:
TestA(){A = 10;}
TestA &operator=(const TestA &ItemCopy)
{
A = ItemCopy.A;
printf("A is:…

SE Does Not Like Dissent
- 1,767
- 3
- 16
- 36
1
vote
0 answers
Upcasting a widget
In a Rust GTK4 application, an event handler needs to send a message to its root, which is a subclass of ApplicationWindow. I can get the root in the handler by "button.property("root").unwrap(), but what I get is a Widget:
Some(
Widget {
…

russell
- 660
- 1
- 10
- 18
1
vote
0 answers
Java upcasting and downcasting problem as follow
I got confused, as the answer suggested option A.
Shouldn't aVehicle bound be viewed as a Vehicle object since the first line operated upcasting?
The question as follow:
Consider the following code snippet:
Vehicle aVehicle = new…

YI Chen Wang
- 11
- 1
- 2
1
vote
0 answers
Can someone explain the use for upcasting via Polymorphism?
I'm in an AP Comp Sci class and the course instructor nor me or my classmates are having any luck understanding upcasting via polymorphism. We more or less understand what it's doing, but not particularly why. Here's some pseudo code as an…

SuperSmasherFan
- 11
- 1
1
vote
2 answers
Overloading assignment operator and upcasting
I was wondering how should I (or can I? does it make any sense?) overload the assignment operator when working with inheritance and upcasting?
Let's say we have Base class and Derived class (inherited from Base). If i have something like:
/// supose…
user15464271
1
vote
1 answer
why the value of ' *ptr ' in the step 3 not equal 3 but 0?
I find that the value of '*ptr' in the steps:3 does not equal 3
#include
using namespace std;
int main(int arg, char* args[])
{
int* ptr;
int x = 4;
float y = 3.142;
cout << y << " " << &y << endl; //step:1
…

Mohamed
- 43
- 1
- 5
1
vote
2 answers
Upcast non-generic ObjC Classes to parent class for Swift method parameter
I have some classes defined in Objective-C similar to this:
@interface Type: NSObject {
}
@end
@interface SubType1: Type {
}
@end
@interface SubType2: Type {
}
@end
@interface Parent : NSObject
@property…

Bogdan
- 402
- 2
- 8
- 18
1
vote
1 answer
How to cast generic collections in F#
Given
type X () = class end
type XX () = inherit X()
type NestedList<'T> = list
- >
let xxs = [[ XX() ]] // list
- >
What is the best way to cast from list
- > to NestedList

Goswin Rothenthal
- 2,244
- 1
- 19
- 32
1
vote
1 answer
Upcasting any pointer to the same derived class instance to a virtual base pointer will always return the same address? C++
I assume the answer is yes but I haven't explicitly found an assertion of this for every possible situation.
Given an intricate inheritance tree that probably would require step by step upcasting due to intermediate non virtual inheritance and also…

SaintJob 2.0
- 554
- 4
- 21
1
vote
3 answers
Creating a Class that creates arrays
I am trying to create a class that creates arrays for a project of mean I have been killing my self on how to do it
public class Team {
private Object arr;
void createTeam(){
String[] arr = new String[15];
}
}

Lynn Long
- 11
- 1
1
vote
0 answers
What does upcasting and downcasting look at? (Object type or Reference type)
I am a beginner and I am confused between these two examples. So sorry if I ask something that has already be answered. I don't know how to search for this issue.
Let's say I have class Person and class Student. Student extends Person.
So when I…

Stefan Jankovic
- 53
- 4
1
vote
2 answers
Java ArrayList Downcasting
So I've been working in a project in which I need to have a list to be filled with its new child class objects.
I've made it with traditional method which one ArrayList will be filled with a type of child Class. But to make the code shorter and…

Dean Debrio
- 57
- 1
- 10