SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
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 ]
Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
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
|