Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

interfaces and subclasses

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
public static void main(String args[]) {
Base base = new Sub(); // 1
I1 i1 = base; // 2
Sub sub = (Sub)base; // 3
I2 i2 = (Sub)base; // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

right answer is e.
i answered b.
at runtime, base refers to an instance of Sub. Sub doesnt implement I1.??
is it bcos it extends base??

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Sub is a subclass of base and thus can be cast to any superclass (class or interface) that base is/implements.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really dont know, what made you to answer "b". Why I say this is, Base implements i1 and I1's refernce is holding the Base's object, sounds valid.

Anyways, what I wanted to generalise is, A reference of a class can hold the objects of itself and its subclasses (without explicit type case) and will compile fine.

Similarly a reference of an interface, can hold all those objects of those classes, that implement that interface. Hope I made it clear for you.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Jayasiji ! I misread your doubt!

Let me extrapolate on this question further ... I have made a bit of modification to the code ... see below ...
===
interface I1
{
public void a1();
}

interface I2
{
public void a2();
}
class Base implements I1
{
public void a1() { System.out.println("In Base, a1() is called"); }
}

class Sub extends Base implements I2
{
public void a2() { System.out.println("In Sub, a2() is called"); }
}

class Yellow
{
public static void main(String args[])
{
Base base = new Sub(); // 1
I1 i1 = base; // 2
Sub sub = (Sub)base; // 3
I2 i2 = (Sub)base; // 4

base.a1();
// base.a2(); // will not compile OBVIOUSLY - a2 not a member of Base.

i1.a1();
// i1.a2(); // will not compile OBVIOUSLY - a2 not defined in i1.

sub.a1();
sub.a2();

// i2.a1(); // will not compile OBVIOUSLY - a1 not defined in i2.
i2.a2();
}
}
===

Hope the above code, gives you the generalised view of what all compiles and runs.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jayashree,

A base class/interface ca reference a derived class/interface, thas not a problem. So there is nothing in this code to run.

Arnab
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
public static void main(String args[]) {
Base base = new Sub(); // 1
I1 i1 = base; // 2
Sub sub = (Sub)base; // 3
I2 i2 = (Sub)base; // 4
}}


when i say //1 , i mean i am creating an instance of Sub() storing the instance in Base. At runtime, base refers to an instance of Sub class.
If my understanding is right, then Sub does nt implement interface i1.
with base as srcRef and i1 as destRef, i have read,

srcRef can be stored in destRef, only if destRef is a superclass or an interface which is implemented. This is neither the case here. Hence, the
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srcRef can be stored in destRef, only if destRef is a superclass or an interface which is implemented



If you ask me u have not understood inheritence in java correctly.To
assign a sub class object to a super class reference vaiable or any other interface reference variable it is not necessary that the sub class should extend the super class or implement the interface itself. As we move up the inheritance tree if any of the super classes implements an interface then the sub class object can be assigned to the interface reference.


From the given code sample it is clear that Sub class extends Base class and Base class implements interface I1 ,so the Base class reference pointing to Sub class object can be assigned to interface I1 reference variable.
[ October 14, 2004: Message edited by: Anand Bheemarajaiah ]
reply
    Bookmark Topic Watch Topic
  • New Topic