• 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:

Assignment of Interface

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. GrapeFruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g // class type to interface ok
4. Grapefruit g2 = squee // cant assign interface
// to class only object
--------------------------------------------------
1. Grapefruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g
4. Grapefruit g1 = new Grapefruit()squee
the resource says implicitly converting an interface to a class is not ok; but JVM will allow at runtime since s is a type Grapefruit.
Would this work then?
Citrus citr = (Citrus)squee
--------------------------------------------------
1. Apple extends Citrus
2. Grapefruit extends Citrus implements Squeez
3. Grapefruit g = new Grapefruit()
4. Squeez squee = g
5. Apple app = (Apple)squee
also states that line 5 will compile but a ClassCastException will occur at runtime. I guess this is only because Apple is not in the heirarchy. line 5 were replaced would this work:
5. Citrus citr = (Citrus)squee
Can someone please demonstrate these three cases?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing to be aware of is that an object may have only one type. In the code below:
1. GrapeFruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g // class type to interface ok
4. Grapefruit g2 = squee // cant assign interface
g (on line 2) has the following types:
- Grapefruit because g is of class Grapefruit
- Citrus because a Grapefruit is_a Citrus
- Object because all objects are of type Object
- Squeez because Grapefruit implements Squeeze
Next we have to think in terms of the runtime class of the object and not the compile-time class, that is, in
Citrus c = new Grapefruit();
c has the runtime type GrapeFruit not Citrus.
on line 3 it is ok to assign g to a reference of type Squeeze because g is_a Grapefruit and class Grapefruit implements Squeeze, that is, g is of type Squeeze too.
on line 4, you cannot assign an object of type Squeeze to a reference of type Grapefruit because there may be another class implementing the interface Squeeze and that has nothing to do with a Grapefruit which results in an incompatible assignment...
From there on, the rest is piece of cake...
HIH
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin,
Any clues about the second case scenerio?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Grapefruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g
4. Grapefruit g1 = new Grapefruit()squee
if you provide correct code I'b be happy to give it a shot (line 4 is not correct java code)
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres the code corrected:
1. Grapefruit extends Citrus implements Squeeze
2. Grapefruit g = new Grapefruit()
3. Squeeze s = g
4. Grapefruit g1 = (Grapefruit)s
RHE p. 124 indicates that the JVM determins s is a Grapefruit and can be converted to a Grapefruit, therefore compiles and runs
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE is right...
Although s is declared as being of type Squeeze, it holds an object of type Grapefruit which makes the casting perfectly legal.
That's why you have to focus on the actual type of the object pointed by the interface reference and not on the interface type itself...
In your third example, line 5 doesn't work because an Apple has nothing in common with a Grapefruit.
HIH
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,
So the first case scenerio is only false due to a lack of casting, since really the first case and second case are otherwise identical.
1st--
1. GrapeFruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g // class type to interface ok
4. Grapefruit g2 = squee // cant assign interface

2nd--
1. Grapefruit extends Citrus implements Squeez
2. Grapefruit g = new Grapefruit()
3. Squeez squee = g
4. Grapefruit g1 = (Grapefruit)squee
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it
note however that as soon as you explicitely cast, it is always gonna compile, but you may run into trouble at runtime...
Remember that casting is telling the compiler "Yeah, man, I know what I'm doing, trust me !"
[ February 07, 2002: Message edited by: Valentin Crettaz ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol thanks, apparently it works for Grapefruits and everything else we've gone thru, so I'll take your word for it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic