• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Type Cast and reference problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base{
public void method(){
System.out.println("Base 1 Method");
}
public void BaseMethod()
{
System.out.println("BaseBMethod Method");
}
}
class Derived1 extends Base{
public void method(){
System.out.println("Derived 1 Method");
}
public void Derived1Method()
{
System.out.println("Derived1 Method");
}
}
class Derived2 extends Base{
public void method(){
System.out.println("Derived2 Method");
}
public void Derived2Method()
{
System.out.println("Derived2 Method");
}
}
public class ClassTest
{
static public void main(String [] args)
{
Base b = new Derived1 ();
Derived1 d1 = new Derived1();
Derived2 d2 = new Derived2();
b = d1;
b.method();
d1 = (Derived1) b;
d1.method();
b.BaseMethod();
//b.Derived1Method(); // Will give error
}
}
1)In this example what b=d1 does can any body answer me please. I'm getting confused which reference assign to which reference
2) d1 = (Derived1) b; How this object is type casted, I mean to which class.
Thnaks,
Kanthu
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear kantu
lemme try
when you say b=d1, you are assigning an object of Derived1 to a reference of Base, which is a widening conversion (child to parent) hence perfectly legal. Later, when you are saying d1=(Derived1)b, what you are doing is trying to force the parent back into a child (narrowing conversion), which is not normal and hence the cast. The compiler accepts it as it sees the cast and assumes that you know what you are doing. It also checks whether the two types (Base and Derived1) have a parent child relationship and finds it OK and hence you do not get any compile time errors. At run time, since the Object referred by b is actually an instance of Derived1 class, your code works fine and you get desired results.
Hope this helps.
Sanjeev
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kantu
In the assignment statement b=d1; d1 (which is an object of Derived1) a subclass of base class, is assigned to a variable of
Base class(the parent class)..so according to "is a" relationship Derived1 "is a" Base so assignment here is OK
And compiler has no objection. And at runtime the method() of Derived1 class is executed which prints "Derived1 Method".
The typecast d1=(Derived1)b; that you have done here is for the compiler as it won't accept "Base is a Derived1" so you have to do this cast to keep the compiler happy although the b variable refers to a Derived1 object already as assigned in the previous statement. Then when d1.method(); is called it runs the same method()so no problem at compile time and no problem at Runtime.
Now...coming to your main issue...when you call
b.Derived1Method();
The compiler looks for such method in Base class and can't find it there and complains although the b variable contains a Derived1 object.
So this behaviour has to be checked keeping the two issues in mind, the Compile time issues and the Runtime Issues..
I hope my exaplination helps you. If anybody else finds this clarification wrong please correct me.
So good luck Kantu
take care
Nasir

 
kantu Deshpande
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nasir and Sanjeev,
Thanks a lot for your explanation. I have one more doubt about the same code. As Nasir said b=d1, b.method(); calls Derived1Method why is it so?. As we are assigning reference from subclass to superclasss, why this b object which is from Base calls derived method instead of calling Base method. Please explain me.
Thanks in advance.
Kantu
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java language has built in polymorphism. When you call a method on a base class reference, it resolves the 'correct' type during runtime and calls the appropriate method of the object it refers to.
 
kantu Deshpande
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by facets:
Java language has built in polymorphism. When you call a method on a base class reference, it resolves the 'correct' type during runtime and calls the appropriate method of the object it refers to.


Thanks Facets,
But still at the run time b and d1 refer to same Base object Am i right?. becoz b=d1 Please correct me if i'm wrong.
Thanks,
Kantu
 
facets
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both of them refer to the same derived object.
 
Ahmed Nasir
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kantu
Lets not call 'b' an object, as it is a variable that has the reference to an object and with b=d1; it starts refering to Derived1 object, (same object as being refered by d1)
and thats why at Runtime its going to execute the method() of Derived1 class.
I think if you clearly distinguish between reference and the actual object then it would be more clear.
So I hope it clarifies some doubts otherwise post your worry and we will try to solve it togather
Thanks
Nasir
[This message has been edited by Ahmed Nasir (edited October 04, 2000).]
 
kantu Deshpande
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Facets and Nashir,
Thanks a lot for your help, both of you. I understood now..
Kantu..
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Facets',
PROPER NAMES ARE NOW REQUIRED!!
Read this post for details.
Ajith
 
reply
    Bookmark Topic Watch Topic
  • New Topic