Questions tagged [downcast]

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

589 questions
-1
votes
2 answers

Comparing types between derived classes

I am making the engine for a game and I can't seem to solve the following problem. So, I have a base component class from which all the different components are derived. A GameObject is basically a container for different components. The components…
Veritas
  • 2,150
  • 3
  • 16
  • 40
-1
votes
2 answers

Is downcast necessary if method is abstract?

Here is an example: public abstract class Solid{ //code...// public abstract double volume(); } Here is a class that extends Solid public class Sphere extends Solid{ //code...// public double volume(){ //implementation// } } Now, if I wanted…
-2
votes
1 answer

upcasting and downcasting in Java and Polymorphism

I'm confused why or when to use downcasting and upcasting and polymorphism. Please tell me if I am correct or wrong. It would be a great help. If we say this Gift gift = new Pen(); not only we can leverage the use of polymorphism (if subclass has…
-2
votes
1 answer

Polymorphism and casting object in Java

is there any possibility that o1 or o2 can be casted to A and the program will run? Why is the last statement a runtime error? public class A{ public A(Object object) { } public A(){ } public String toString(){ …
kryon16
  • 1
  • 3
-2
votes
1 answer

How to call a derived class method using a base class object?

I understand how this goes in the opposite direction. But for various reasons i want to use a base class object to call a derived class method Let's say we have 2 classes, together representing a person's data (name and age) : class Person…
RedIcs
  • 11
  • 5
-2
votes
3 answers

Why can't I downcast this object in Java?

I have this method: private Message getMessage(DataInputStream in) throws IOException { CommandEnum caption = CommandEnum.valueOf(in.readUTF()); BasicMessage inputMessage; if (caption.equals(CommandEnum.BEGIN) ||…
yotamoo
  • 5,334
  • 13
  • 49
  • 61
-2
votes
1 answer

downcasting not working and giving error in my program

I have a class Person and a class Instructor that extends Person public class Driver { public static void main(String args[]) { Person[] array = new Person[5]; array[1] = new Person("John Doe"); array[2] = new Person("Bobby Gram"); …
-2
votes
1 answer

How to avoid force downcasting and unwrapping optionals without nested loops in Swift?

I'm having a difficult time trying not to write nested loops whenever I avoid force downcasting and unwrapping optionals. Is there a way to do this? Example: var customers = [Customer]() if response.result.isSuccess, let jsonDictionary =…
kevinnguy
  • 454
  • 4
  • 13
-2
votes
1 answer

Downcasting in Java ? Is this a downcasting issue at all?

So I have a POJO class let´s call it: InnerDomainObject. Then I have an object representing this object, with a few more fields, for communication towards different clients (it s an API DTO): OuterDomainObject Because the DTO has in fact all of the…
noneconnex
  • 29
  • 6
-2
votes
1 answer

Swift: Casting [UInt32] to AnyObject

I have a UInt32 array of Ints defined as: var myArr : [UInt32] = [1, 2, 3] how can I convert it to AnyObject type ? I've tried the forced downcast as! AnyObject but the compiler gives this warning: treating a forced downcast to AnyObject as…
JAHelia
  • 6,934
  • 17
  • 74
  • 134
-2
votes
2 answers

Why I am getting swift Dynamic Cast failed?

This line of code seems to be causing the problem { gymnastTables.gymnastsArray = defualts.objectForKey("Gymnasts") as Array } Why I am getting a downcast error?
Suneet Tipirneni
  • 839
  • 1
  • 12
  • 17
-2
votes
2 answers

Alternatives to downcasting when implementations have unique methods

I have the following Issue. Where I have to cast inside of the makeLeftTurnMethod... this looks very ugly to me.. Is there a way where I don't have to do this? public interface Car(){ public void turnRight(); public void turnLeft(); public…
-3
votes
1 answer

Is this upcasting or downcasting?

int main( ) { Base *ptrBase = new Derived( 50, 60, 70 ); Derived *ptrDerived = ( Derived*)ptrBase; ptrDerived->printRecord( ); delete ptrBase; return 0; }
user15023865
-3
votes
2 answers

Confusing about downcast in c#

I have two classes: public class Asset { } public class Stock : Asset { ... } When I write: Stock m = new Stock (); Asset а = m; Stock s = (Stock) а; Everything works fine! But when I write in this way: Asset а = new Asset(); …
Edward0802
  • 13
  • 1
-3
votes
4 answers

Virtual destructor of base class is not called when downcasting is done via dynamic cast

When I run the following code, I get the following issue. Deleting derived pointer d1 does not call the destructor of the base class. Deleting derived pointer d2 calls the base destructor. Why is the type of cast (dynamic or static) affecting…
Pranav Kapoor
  • 1,171
  • 3
  • 13
  • 32
1 2 3
39
40