upcasting

Why Java object class remains same after casting?

*爱你&永不变心* 提交于 2021-02-18 05:34:06
问题 I tried to upcast an objet. But at runtime object class is remained as a derived class. Derived drv = new Derived(); Base base = (Base) drv; System.out.println("Class : " + base.getClass()); //prints -> Class : class packagename.Derived So Why class property didn't change? 回答1: So Why class property didn't change? Because the object hasn't changed, just the type of the reference you have to it. Casting has no effect at all on the object itself. In Java, unlike some other languages (thankfully

confusion about upcasting vs dynamic binding

北慕城南 提交于 2021-01-05 07:13:57
问题 So I am having some confusion understanding dynamic binding vs upcasting. My original understanding was that when you create a base_class reference and assign it to a derived class object like: base_class obj = new derived_class(); you can now call the methods/member functions of derived_class with obj.method_of_derived class() , without dynamic binding. I thought you only needed dynamic binding when the base_class and derived_class have functions/methods that have the same name, and some

Upcasting/Downcasting in Java

痞子三分冷 提交于 2020-02-14 02:32:30
问题 I am trying to understand upcasting and downcasting in Java and I am confused by the following scenario (about my code, which is below): First - why is it that the code does not compile when I include the line myAnimal.bark(); , and Second - (assuming I comment out myAnimal.bark(); ) why does calling myAnimal.move() print "moveDog" instead of "moveAnimal" ? Isn't myAnimal restricted to methods from the Animal class because we have declared its type to be Animal , even though we are setting it

Upcasting in Java and two separate object properties

时光怂恿深爱的人放手 提交于 2020-01-23 17:00:00
问题 Trying to understand upcasting in Java. Recently observed strange behavior. Example: public class A extends B { public int i = 2; public void printI() { System.out.println("print i = " + this.i); } public static void main(String[] args) { B a = new A(); // <- upcasting here System.out.println("i = " + a.i); a.printI(); } } class B { public int i = 1; public void printI() {} } //Output: //i = 1 //print i = 2 Seems, that upcasted object has two separate "i" properties. One "i" accessible

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

不打扰是莪最后的温柔 提交于 2020-01-21 07:45:08
问题 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(){ System.out.println("Example::Method_2"); } } public class Test extends Example{ public void methodTwo(){ //Method overriding System.out.println("Test::Method_2"); } public void methodThree(){ System.out.println("Test::Method_3"); } public static void main(String[] args){ Example exa = new Test(); //

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

北城以北 提交于 2020-01-21 07:44:27
问题 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(){ System.out.println("Example::Method_2"); } } public class Test extends Example{ public void methodTwo(){ //Method overriding System.out.println("Test::Method_2"); } public void methodThree(){ System.out.println("Test::Method_3"); } public static void main(String[] args){ Example exa = new Test(); //

Why upcasted ab.a + ab.b produce result 1 and 1.0? [duplicate]

懵懂的女人 提交于 2020-01-11 14:20:34
问题 This question already has answers here : Is there a way to override class variables in Java? (17 answers) Why is super class constructor always called [duplicate] (3 answers) Closed 6 days ago . I don't get how we managed to invoke constructor without parameters in class A at all. How upcasting works in this particular example? When we produce A ab = bb; what exactly ab refers to? public class A { public Integer a; public Float b; public A() { a = 1; b = 1.0f; } public A(Integer x) { a = 2; b

How to implement a compile-time check that a downcast is valid in a CRTP?

做~自己de王妃 提交于 2020-01-02 02:33:10
问题 I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast

Java - Upcasting and Downcasting

帅比萌擦擦* 提交于 2020-01-01 19:40:16
问题 I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that. Upcasting - Conversion from child to parent - Compiler takes care. No cast is required Downcasting - Conversion from parent to child - Explicit cast is required public class Animal { public void getAnimalName(){ System.out.println("Parent Animal"); } } public class Dog extends Animal{ public