• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Question on casting

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to R&H book the following are run time rules for object casting.
newtype nt;
oldtype ot;
nt=(newtype)ot;
1.If newtype is class the class of the expression being converted to must inherit from newtype.
2.If newtype is Interface,the class of the expression being converted to must implement that interface.
What about the following scenario?
When old type is interface & new type is class
What is the run time rule for object casting in the above scenario?
Thanks
Veena
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena
Does this help ?

Sri
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u have given me example of when oldtype is class & newtype is interface,but I have asked viceversa situation.I mean oltype is interface & newtype class.I want some defined rule.
Thanks
Veena
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody answer to this question?I can easily solve casting questions other than the scenario I explained above.I don't know exactly what rule applies when casting from interface type to class type.
Thanks
Veena
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what the JLS says about Casting Conversions: http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#175725
If you find that a bit too formal and confusing...
Using an interface reference to refer to an object does not change that object's runtime type. Therefore, the runtime type of the object referred to by the interface reference should be assignment compatible with the object reference you are assigning it to.
That is, if you have
List someList = new ArrayList();
Then,
ArrayList obj1 = (ArrayList) someList; // legal - the object being referenced is an ArrayList
AbstractList obj2 = (AbstractList) someList // legal - the object being referenced is a subclass of AbstractList
LinkedList obj3 = (LinkedList) someList // runtime error - the object referenced is an ArrayList which is not assignment compatible with LinkedList

There are additional restrictions when dealing with references to final classes but I'll leave that as an exercise for you.
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could try to guess:
how could you cast an interface if the interface cannot be istantiated?
Marco
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually concept is still not clear to me.
Veena
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to be clever with another example.
Let's assume you have:
MyInterface myInt = new MyInterfaceImpl();//This creates an object MyInterfaceImpl of type MyInterface
What actually happens here is that at runtime first an instance of MyInterfaceImpl is being created and then an upcasting to its interface is done.
The same result could be accomplished doing the following:
MyInterfaceImpl myObj = new MyInterfaceImpl();
MyInterface driver = (MyInterface)myObj;
Since you cannot create an instance of an interface like:
MyInterface myInt = new MyInterface();
as an interface cannot accomplish any work because its methods don't contain any implementation and therefore there is no logic in creating an instance of an interface
trying something like:
MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible.
Hope it will help,
Marco
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Marco Tedone:

...
The same result could be accomplished doing the following:
MyInterfaceImpl myObj = new MyInterfaceImpl();
MyInterface driver = (MyInterface)myObj;

The cast is not required. As long as myObj implements MyInterface, the following are equivalent:
// 1
MyInterface driver = new MyInterfaceImpl();
// 2
MyInterfaceImpl myObj = new MyInterfaceImpl();
MyInterface driver = myObj;


MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible.

Such a casting is in fact possible. In a way, you are right: You are not actually casting the interface, but the object behind the interface. Remember, an interface reference is just another way of "seeing" an object. You only "see" what the interface defines. The underlying object can have other capabilities. Which is why this would work:

Hope this helps clarify the concepts a bit.
[ February 12, 2003: Message edited by: Junilu Lacar ]
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Junilu Lacar:
Originally posted by Marco Tedone:

...
The same result could be accomplished doing the following:
MyInterfaceImpl myObj = new MyInterfaceImpl();
MyInterface driver = (MyInterface)myObj;

The cast is not required. As long as myObj implements MyInterface, the following are equivalent:
// 1
MyInterface driver = new MyInterfaceImpl();
// 2
MyInterfaceImpl myObj = new MyInterfaceImpl();
MyInterface driver = myObj;


MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible.

Such a casting is in fact possible. In a way, you are right: You are not actually casting the interface, but the object behind the interface. Remember, an interface reference is just another way of "seeing" an object. You only "see" what the interface defines. The underlying object can have other capabilities. Which is why this would work:


Hope this helps clarify the concepts a bit.
[ February 12, 2003: Message edited by: Junilu Lacar ]

 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic