Questions tagged [upcasting]

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

244 questions
8
votes
2 answers

In Java, why does type casting of a character to an integer NOT extend the sign bit

In Java a bitwise operation causes type casting to integer and also causes sign extension. For instance the following is expected: byte b = -1; System.out.println(b >> 1);//-1 In Java chars are encoded in UTF-16 and each unit is represented with 2…
CEGRD
  • 7,787
  • 5
  • 25
  • 35
8
votes
10 answers

Does up casting in Java hide the subclass methods and fields?

On the program I'm writing I have a class RestrictedUser and class User that is derived from RestrictedUser. I'm trying to hide the User specific methods by casting to RestrictedUser but when I do the casting the User methods are still available.…
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
8
votes
1 answer

Placement new and inheritance

Good evening everyone. A code snippet will be worth a thousand words : // Storage suitable for any of the listed instances alignas(MaxAlign::value) char storage[MaxSize::value]; // Instanciate one…
Quentin
  • 62,093
  • 7
  • 131
  • 191
8
votes
1 answer

what is "upcast"?

I can see that the term "upcast" is related to OOP, but I can't find the exact definition by searching the Internet. Could anyone explain what does the term mean and in what situation this technique useful?
can.
  • 2,098
  • 8
  • 29
  • 42
7
votes
5 answers

Cost of Up-casting to ArrayList of objects and then down-casting to custom ArrayList

I have a situation in which I am getting data from database, and I want to upcast it to ArrayList of objects and then downcast it to different custom ArrayList i.e. List, List etc. My question is up-casting to object and then…
Androider
  • 2,884
  • 5
  • 28
  • 47
7
votes
7 answers

How to cast object to its actual type

Consider the following piece of code: class MyClass { } class MyClass2 : MyClass { } private void Foo(MyClass cl) { //cl is actually MyClass2 instance TestGeneric(cl); } private void TestGeneric(T val) { //do smth } After calling…
Gena Verdel
  • 588
  • 5
  • 21
6
votes
3 answers

C++ - upcasting and downcasting

In my example: At upcasting, shouldn't the second d.print() call print "base"? Isn't it "d" derived object upcasted to a base class object? And at downcasting, what advantages does it have? Could you explain upcast and downcast in a practical…
Mihai
  • 101
  • 2
  • 2
  • 8
6
votes
2 answers

Use a function with an argument of a derived type (F#)

I've got a class in my application - for simplicity let's assume it's defined like this: type baseType() = member this.A = 5. Also, I've got a lot of functions that take objects of this type as an argument. Moreover, some of them take an array…
LA.27
  • 1,888
  • 19
  • 35
5
votes
8 answers

up-casting in C# and call a specific method based on the derived type

I have a couple of classes, all derived from the same base type. class basetype{} class TypeA : basetype{} class TypeB : basetype{} ... A number of them is stored in a list. List myObjects As always, each of these types has to be handled…
Alexander
  • 145
  • 1
  • 1
  • 6
5
votes
1 answer

Is overriding toString() considered polymorphism?

I was having an exam in Java today and the examiner asked me if I can provide any examples of using polymorphism in my Spring Boot project. As I couldn`t think of anything at first, he pointed out that I have overriden toString() in my models and…
Petar Bivolarski
  • 1,599
  • 10
  • 19
5
votes
7 answers

Upcasting when making object

Say you have a Shape base class and various derived types: Circle, etc. Is there ever any reason to upcast right there when making a new object, by writing this: Shape s = new Circle(); instead of this: Circle s = new Circle(); and are the s…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
5
votes
3 answers

why overridden method calling from Subclass if i have done up-casting?

i have just started learning java::Inheritance and confused while mixing Up-Casting. class Example{ public void methodOne(){ System.out.println("Example::Method_1"); } public void methodTwo(){ …
Avinash Kumar
  • 288
  • 6
  • 21
5
votes
1 answer

Cast for upcasting only

We all know that C-style casts are considered evil in C++. Which is why they are replaced by const_cast<>, static_cast<>, and dynamic_cast<> to provide for more bespoke casting, allowing the programmer to only allow the intended classes of…
cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
5
votes
2 answers

C#: Superclass still has subclass info after upcast

I have 2 types: BaseQuestion and Question. Question inherits some properties from BaseQuestion. Now I have created a Web API to make a BaseQuestion available. The Question datatype has additional properties that I do not want to make available. I…
Jan
  • 2,168
  • 2
  • 19
  • 28
4
votes
2 answers

What are the undesirable results the author is talking about?

This example was taken from Bruce Eckel's "Thinking in C++" Chapter 14, Section "Upcasting and the Copy Constructor". #include using namespace std; class Parent { int i; public: Parent(int ii) : i(ii) { cout << "Parent(int…
Belloc
  • 6,318
  • 3
  • 22
  • 52
1
2
3
16 17