• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface(URGENT)

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let's say I have Interface class A and I have another class B which Implements Class A.
(1)So can I say Interface class are always superclass of class(which implements interface)?
(2)Then what is difference between extends and implements?
Thanks,
Angela
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interfaces and classes are two different things...there is no such thing as an "interface class."
A class you define can extend ONE class, and/or implement one or more interfaces.
An interface you define can extend one or more interfaces. It obviously can't implement any interfaces, because interfaces are abstract and contain no implementation.
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey Let's say I have two classes & one interface:
Interface A
class B
class C
class D
Now class B implements A
{
}
class C
{
B b;
class D = new class(b);
}
class D
{
in constructor D(interface A);
}
Can I do this? Technically why? Is this because Class B subclass of Interface A???
HTH,
Thenks,
Angela
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try writing up your question in real java code and compiling it, and seeing what the compiler does.
Also, class B is not a subclass of anything. Class B implements the interface A. ONLY interfaces can extend other interfaces, and then you don't say one interface is a "subclass" of another, you say it's a "subinterface". Keep these two concepts separate.
[ April 25, 2002: Message edited by: Rob Ross ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it because B is an A.
Let's put names to these things.
Let's make class B an Apple and interface A, things with seeds:

interface ThingsWithSeeds {}
class Apple implements ThingsWithSeeds {}
class Garden {
___void plantSeeds(ThingsWithSeeds s) {}
}
class Test {
___public static void main(String[] args) {
______Apple apple = new Apple();
______Garden garden = new Garden();
______garden.plantSeeds(apple);
___}
This works because Apple is a ThingWithSeeds.
note that the underscores are used only to help with spacing. The code tag makes the code too small to read.
Notice that you could also code it this way:
class Test {
___public static void main(String[] args) {
______ThingsWithSeeds seeds = new Apple();
______Garden garden = new Garden();
______garden.plantSeeds(seeds);
___}
[ April 26, 2002: Message edited by: Thomas Paul ]
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
You can try writing up your question in real java code and compiling it, and seeing what the compiler does.


I compiled works fine. But one question I have is then what is difference between Subinterface and child or subclass?
Thanks
Angela
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela, a sub-interface extends another interface, while a subclass extends another class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic