Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
0
votes
0 answers
How is this casting happening in the following java code
I do not understand how the casting is done on var2
The following line does not Compile as methodA1() not present in interface reference I1
String var = i1.methodA1();
Hence I1 requires a cast to invoke methodA1
I do not understand how the casting…

Anshul Gupta
- 265
- 2
- 12
0
votes
1 answer
Java parent's private attribute inside child constructor
The title says it all, I got a class in which the variables of the constructor must be private.
public class AdMedia {
private String name;
private int price;
public AdMedia(){}
public AdMedia(String name, int price) {
this.name = name;
…

Dean Debrio
- 57
- 1
- 10
0
votes
1 answer
Why does TypeScript allow implicit downcast in method parameters?
Here is the example:
interface Vehicle{
mass:number
}
interface InspectorClass{
inspect(v:Vehicle):void
}
class Car implements Vehicle{
mass = 2000
wheels = 4
}
class Boat implements Vehicle{
mass = 3000
sails = 2
}
//…

Mircode
- 432
- 5
- 12
0
votes
1 answer
Android Java class casting
I am wondering if someone could explain something about a class cast for me.
I am playing around with Android and I have a subclass of Application named ExApp.
I want to call a method of ExApp from one of my activities, so I do:
ExApp ex =…

Tom
- 1
- 1
- 1
0
votes
1 answer
Downcasting Polymorphism and assignments
I have created a toy problem to understand why I am unable to set the value of a property after downcasting. The issue is in view controller #3: I am wanting to assign a value to property artist.
import Foundation
protocol MediaType {
var…

fingia
- 504
- 7
- 20
0
votes
1 answer
Reuse code that should be run on base object type with templates in overloaded virtual function for derived object types
Context:
I got the next hierarchy of structers:
Event -> MouseEvent -> MouseButtonEvent
-> MouseWheelEvent
-> ...
-> KeyboardEvent -> KeyEvent
-> InputEvent
…

maxnevans
- 1
- 3
0
votes
0 answers
Casting from superclass to subclass fail in unit test (mockito)
I thought I know the basic idea of upcast & downcast, but this problem I encountered make me think again.
Currently, it compiled and work flawlessly. But when I try to do the unit test (by using mockito), it keeps pointing that my way of casting…

Airul Fmy
- 3
- 2
0
votes
2 answers
Passing `List` of value types to a method expecting `IEnumerable` of reference types
I am wondering why it is not possible in .NET to pass in a list of value types to a method which expects an IEnumerable of reference types. For example,
void MyMethod(IEnumerable

E. Shcherbo
- 1,128
- 8
- 15
0
votes
1 answer
Eliminating forced downcasts
I am using swiftlint as a way to code better, and for the most part I have been able to muddle my way through up to the following code which includes a forced downcast:
let questionToAdd = MultipleChoiceQuestion(question: dictionary["Question"] as!…

Douglas W. Palme
- 390
- 1
- 4
- 10
0
votes
2 answers
Avoiding forced downcast from Any to CFString
So here's some code I found from another question, it has a forced downcast from Any to CFString
import SystemConfiguration.CaptiveNetwork
func fetchSSIDInfo() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces()…

Yogurt
- 2,913
- 2
- 32
- 63
0
votes
2 answers
Why downcast array item in Swift?
Why must I downcast an array item in Swift, if the sub-class of the array item is known?
> class B {let t: String = "-B-"}
> class B1:B {let t1: String = "-B1-"}
> class B2:B {let t2: String = "-B2-"}
> let bunch = [B(), B1(), B2()]
As…

P2000
- 1,042
- 8
- 14
0
votes
3 answers
Calling Derived class function from a Base class pointer after typecasting it to Derived class pointer
I am fairly new to C++ (& OOP). I am struggling to understand the following piece of code:
#include
class Base {
public:
Base() {
std::cout << "In Base Constr: " << __FUNCSIG__ << std::endl;
}
virtual ~Base() {
…

RamBo
- 15
- 4
0
votes
3 answers
How to downcast an array in Java?(Polymorphism)
I currently have a Supercar class that extends a Car class, and want to downcast an array of Car class.
My code:
System.out.println("\nLab Task 6");
Car[] cars = new Car[10]; //object array
cars[0] = new SuperCar( model: "Lambo", cc: 8000, type:…

Oscar Chua Wei Wen
- 57
- 1
- 8
0
votes
1 answer
Function returning dynamicly casted pointer, return pointer as if it wasn't casted at all
I have a base class of Component and mulitple deriving classes like MeshComponent. The Entity class stores all Components as std::shared_ptr in std::unordererd_map caaled m_Components, so I create the following function to get component from the…

Szahu
- 43
- 5
0
votes
2 answers
Why downcasting is not allowed in C#?
i've 2 classes called EventDto and EventWithObjectsDto. EventDto is the parent class and EventWithObjectsDto inherits from it.
These are the classes:
public class EventDto : BaseDto
{
public EventTypes Type { get; set; }
public string Title…

Sirolol
- 77
- 9