0

I am new to Java and got a doubt regarding Upcasting and Down-Casting. I have written the following code:

class Bike{
 int speedlimit=90;
 int bikeId = 123576;
  Bike(){  
  System.out.println("inside Bike");} //end constructor

 public void run(){ System.out.println("Bike is running");
   }// end run 

 };//end Bike 


class Honda3 extends Bike{
 int speedlimit=150;
 int bikeId = 456;

 Honda3(){    System.out.println("inside Honda");} //end constructor

 public void run(){ System.out.println("Honda is running");
 }// end run 

  public static void main(String args[]){
   Bike obj=new Honda3();
   System.out.println(obj.speedlimit);
   System.out.println(obj.bikeId);
   obj.run();
   Honda3 obj2 = (Honda3) obj;
   obj2.run();
   obj.run();
   }  // end main
 };//end honda3

Output:

inside Bike
inside Honda 
90
123576
Honda is running
Honda is running
Honda is running

When I created Object obj It was upcast to Parent Class Bike, it shows or describes the variables declared inside the parent class and not in itself. But then it does not execute the run method inside the Parent Class i.e. Bike but it still executes the one in Honda3 class.

So my doubt is, whenever any object is upcast to its parent class does it lose all its properties or variables?

And also why can't an Upcast object cannot access parent method i.e. using the following?

`Bike obj = new Honda3(); // Output should be : Bike is Running` 
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

4 Answers4

1

Let me explain you this.

A. Bike obj=new Honda3(); Here, you are creating the object of Honda3 class not Bike class. So the reference type of obj is Bike and object type is Honda3

B. You are getting below output because when you call new Honda3() the constructor of Honda3 will be called. The first statement in default construcor is always super()(which is construtor of Bike). Even if you don't put it, it will be put my compiler.

inside Bike
inside Honda

C. Access to fields is always determined by the static type. This is in contrast to methods which are determined by the runtime type of the actual object.In other words, for field access the compiler determines at compilation time what is going to be accessed. Hence, these values are from Bike.

90
123576

D. For,

obj.run();
Honda3 obj2 = (Honda3) obj;
obj2.run();
obj.run();

You have overridden the run method in Honda3. So its a runtime polymorphism. As the object type is Honda3 that's why you see.

Honda is running
Honda is running
Honda is running

Hope this helps!

ProgrammerBoy
  • 876
  • 6
  • 19
  • the static type for obj is Bike...so these values will be taken from Bike class not Honda3...these gets bind at compile time not run time. – ProgrammerBoy Apr 21 '17 at 10:13
  • I did but it's not happening, says I don't have enough score to upvote and set the answer. – Raj Sawant Apr 24 '17 at 06:01
0

Output should be : Bike is Running

Why? Your object type is a Honda, not a bike.

This is up casting.

((Bike) obj).run()

Though, instead of that syntax, I think you're looking to call super.run() from the Honda class

Possibly more info: Why do we assign a parent reference to the child object in Java?

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

The method called on an object is not determined by the type of the variable, but by the type of the object, which will be Honda3, if that is what you instanitated. You cannot achieve the output Bike is running from a Honda3 instance.

kalsowerus
  • 998
  • 8
  • 23
0

First question, regarding casting an object to a parent object:

https://stackoverflow.com/a/16224294/6103979

Second question, regarding running super run():

https://stackoverflow.com/a/1032906/6103979

Community
  • 1
  • 1
Gnewbie
  • 21
  • 5