Skip to main content

Questions tagged [upcasting]

Upcasting permits an object of a subclass type to be treated as an object of any superclass type.

upcasting
0 votes
2 answers
125 views

Is C# element inside Tuple cannot be upcasted? [duplicate]

I'm encountering a compilation error in C# .NET 8.0 when trying to pass a Dictionary with a tuple as its key to a method. The tuple consists of a base class and another class, but when I try to use a ...
K.R.Park's user avatar
  • 1,099
1 vote
1 answer
90 views

Upcasting trait in an option field

I am just learning Rust and have some issues with upcasting a trait ref inside an option. I want to pass it as ref to reduce the overhead. The basic idea is as follows: I have some objects (children) ...
Sapp's user avatar
  • 31
0 votes
1 answer
74 views

Up Casting Using References Instead Of Pointers

I wonder if the following up-casting is valid? And if yes, is it possible to down-cast in the same way? class A { public: A(){ cout << "A constructor\n";}; A(const A& a){ ...
Ahmed Waleed's user avatar
1 vote
3 answers
82 views

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 ...
Ajinkya Ronghe's user avatar
-2 votes
1 answer
202 views

Upcasting and downcasting in Java and polymorphism

I'm confused why or when to use downcasting and upcasting and polymorphism. Am I correct or wrong? If we say this Gift gift = new Pen(); not only can we leverage the use of polymorphism (if subclass ...
Sourav Karjole's user avatar
0 votes
1 answer
56 views

Clarifying some relationships between types, upcasting, and classes/interfaces [duplicate]

Everything occurs in Java. I'm not sure to what extent the programming language affects my questions, and I would be interested to know. Suppose I have a class Animal, and a subclass which extends ...
Mr. Brown's user avatar
  • 111
0 votes
2 answers
54 views

Populating a list from a class with objects from other classes

I am having the next code arhitecture: class candybox with arguments flavor and origin which is the parent class for classes: Lindt, Baravelli and ChocoAmor which will have as a plus arguments: ...
Aled's user avatar
  • 1
0 votes
2 answers
58 views

Will upcasting classes affect the method call? Why? Security issues? [duplicate]

Let's have the specified code: class Foo { public void doSomething(){ System.out.println("Foo"); } } class Bar extends Foo{ @Override public void doSomething(){ ...
acrastt's user avatar
  • 106
0 votes
2 answers
140 views

In scala 3, is it possible to make covariant/contravariant type constructor to honour coercive subtyping?

This is a simple example: object CoerciveCovariance { trait Cov[+T] def cast[A, B](v: Cov[A])( implicit ev: A <:< B ) = { v: Cov[B] } } It doesn't compile: ...
tribbloid's user avatar
  • 4,028
0 votes
1 answer
95 views

Using protected method of the parent class in another package

I have Tadpole class that extends Frog, as follows: 1: package animal; 2: public class Frog { 3: protected void ribbit() { } 4: void jump() { } 5: } 1: package other; 2: import animal.*; 3: ...
motodev's user avatar
  • 51
0 votes
2 answers
77 views

Can't cast from a child ref to a parent ref in C#. Why not? [duplicate]

I am new to using C#. I am most familiar with C++ and Java when it comes to Object-Oriented Programming and polymorphism. Can someone tell me why this upcast does not work in C#, and what I can do to ...
Raisintoe's user avatar
  • 201
1 vote
0 answers
82 views

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")....
russell's user avatar
  • 698
1 vote
0 answers
38 views

Java upcasting and downcasting problem as follow [duplicate]

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 ...
YI Chen Wang's user avatar
-3 votes
1 answer
70 views

Why java upcasting causes overwritten girl method to be called? [closed]

I’m trying to understand upcasting with Java. I just created two classes Mom and Girl, with each having their own constructor (constructors have no parameters). If I understand, Mom mom = new Girl() ...
Idash's user avatar
  • 65
0 votes
1 answer
325 views

Java accesing properties with same name in both classes

I have 2 classes, A and B which B inherits from A. Both classes have a property of type int called w. In class A w is public and in class B w is private. I made an object of type A using B constructor ...
dudex198's user avatar

15 30 50 per page
1
2 3 4 5
17