Solved

upcasting down casting object remains same but refernece changes

Posted on 2016-08-16
8
56 Views
Last Modified: 2016-08-16
Hi,

When we do upcasting down casting and object instance why actual object still remains same but referenece changes? please advise
0
Comment
Question by:gudii9
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41758132
Please link to where you read that.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758153
public class Casting {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Derived drv = new Derived();

		Base base = (Base) drv;

		System.out.println("Class : " + base.getClass()); 

		
	}

}

Open in new window


public class Derived extends Base{

}

Open in new window


public class Base{

}

Open in new window


output is
Class : class Derived
i expected Base??
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41758158
i expected Base?
Why?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758170
package com.upcast;
class Base {
    public Base() {}
    public void foo() {
        System.out.println("I'm the base!");
    }
}

Open in new window

package com.upcast;

class Child extends Base {
    public Child() {}
    public void foo() {
        System.out.println("I'm the child!");
    }
}

Open in new window


package com.upcast;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Child x = new Child();
		Base y = (Base) x;
		y.foo();
	}

}

Open in new window

output
I'm the child!
i expected I'm the base! (as reference is Base type)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:gudii9
ID: 41758172
i expected Base?
Why?

since we upcaste to Base from Derived?
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 41758193
Casting does not change the type of an object. It only changes the type of the reference to it. No matter how much casting you do to an object, calling getClass() on it will return the same class.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758228
is it same with down casting? any good  example on down casting? do we do down casting also often as upcasting?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41758240
It's the same with any casting.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now