Questions tagged [upcasting]

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

244 questions
4
votes
1 answer

Converting a Derived Class to Base Class for WCF Serialisation

I have two classes... [Serializable] [DataContract] public class A { [DataMember] public string _a { get; set; } [DataMember] public bool _b { get; set;…
ornah
  • 41
  • 2
4
votes
4 answers

Problem in upcasting in Java?

Could someone please explain why this happening: class Apple { String type; setType(){ System.out.println("inside apple class"); this.type = "apple"; } } class RedApple extends Apple { String type; setType() { …
Larry
  • 11,439
  • 15
  • 61
  • 84
4
votes
5 answers

Why is upcasting a Class not changing overridden methods?

I have a subclass ScottishPerson which inherits from the class BritishPerson. class BritishPerson { public String name = "A british name"; public void salute() { System.out.println("Good Morning!"); } } class ScottishPerson…
TheValars
  • 281
  • 2
  • 10
4
votes
1 answer

Up casting and down casting confusion in Swift?

class Media { var name :String = "" init(name:String) { self.name = name } } class Song:Media {} class Movie:Media{} let s1 = Song(name :"Fireproof") var m1 :Media = s1 //upcasting //var s2 :Song = m1 var s2:Song = m1 as Song…
Ali
  • 505
  • 4
  • 12
4
votes
4 answers

cannot convert from 'DerivedClass *' to 'BaseClass *&'

I try the following code, and get a compilation error. class A {}; class B : public A {}; void f(A*& p) {} int main() { B* pB; f(pB); // cannot convert argument 1 from 'B *' to 'A *&' return 0; } How to work around to make f(pB)…
user1899020
  • 13,167
  • 21
  • 79
  • 154
4
votes
3 answers

memory allocation for upcasting in java

Considering these classes: public class Animal{ } public class Dog extends Animal{ } public AnimalTest(){ public static void main(String[] args){ Dog d = new Dog(); Animal a = d; } } my question is since I performed an…
anathema
  • 947
  • 2
  • 15
  • 27
4
votes
4 answers

Upcasting and Downcasting confusion in java

Okay. So if... int x=3; int y=5; x=y; That'll make x=5, right? Okay, so if B is a subclass of A... A a=new A(); B b=new B(); a=b; ^^^Why is this considered upcasting? Isn't the "a" supposed to become the "b" and not the other way around? Can…
ABCD123
  • 137
  • 1
  • 6
4
votes
2 answers

unique_ptr upcast in return

I have this function that's supposed to generate different derived objs and return as a unique_ptr: class base {}; // contains pure functions. class derived1 {}; // reifies that pure iface. class derived2 {}; // reifies that pure…
Haix64
  • 806
  • 1
  • 13
  • 21
4
votes
2 answers

Deleting an array of objects upcasted to base pointers

started moving some libraries from msvc to mingw, and found really interesting behavior of msvc when one wants to delete an array of upcasted objects. Namely msvc does some dark magic (it seems to love doing that) and the bellow code executes just…
Honf
  • 215
  • 1
  • 6
4
votes
1 answer

FSharp and upcasting to Interfaces seems redundant

I have the following code snippet using the reactive extensions: let value : 't = ... Observable.Create<'t>(fun observer -> let subject = new BehaviorSubject<'t>(value) let d0 = subject.Subscribe(observer) let d1 =…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
3
votes
6 answers

Use methods declared in implementation that are not defined in interface

I have a class defined by an interface public interface Test { void testMethod(); } Test test = new TestImpl(); public class TestImpl implements Test { @Override public void testMethod() { //Nothing to do here } …
ryandlf
  • 27,155
  • 37
  • 106
  • 162
3
votes
4 answers

Upcast an object

I am searching a way to get the possible types for upcasting an object. For example: I have a control of type MyControl which inherits Control. Now, when the object of type MyControl is downcasted to Control is there a way to find out, if it is the…
BennoDual
  • 5,865
  • 15
  • 67
  • 153
3
votes
4 answers

What are the disadvantages of "upcasting"?

The purpose of an abstract class is not to let the developers create an object of the base class and then upcast it, AFAIK. Now, even if the upcasting is not required, and I still use it, does it prove to be "disadvantageous" in some way? More…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
3
votes
0 answers

Upcast instance to parent class in python

(How) Is it possible in Python to treat an instance of class B exactly as an instance of class A, where A is a parent of B (like up-casting in compiled languages)? Say we have the following: class A: def __init__(self, prop=None): …
3
votes
1 answer

automatic upcast when you call function with null

This code prints out MyUrgentException. Could anybody explain why? class MyException extends Exception{ } class MyCriticalException extends MyException{ } class MyUrgentException extends MyCriticalException{ } public class Test{ public void…
Fedor Skrynnikov
  • 5,521
  • 4
  • 28
  • 32
1 2
3
16 17