This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes inheriting interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheriting interfaces" Watch "inheriting interfaces" New topic
Author

inheriting interfaces

P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
Hello,

Does a subclass inherit the interfaces that base classes implements?

In particular, if I have a base class implementing 'Serializable', does subclass automatically implement it?

Thanks,
P Ingle


Thanks,<br />P.Ingle
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch! In this forum we often encourage you to answer your own questions a little bit at a time. For this one, why not just try it? Make an interface I with one method, a class A that implements I and a class B that extends A. Then see if ( B instanceOf I ).

Also try saying "A implements I" but don't actually code the required method in A. What does the compiler tell you then?

Don't be discouraged by this answering a question with a question. Or two. We really want you to have the joy of working things out. They'll stick in your head much better that way! Let us know what you find and whether it makes sense.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
Thanks for the suggestion. My qusetion was related to serializable (or similar) interface that does not have any method of it's own. I have BaseClass inplementing Serializable and SubClass extending BaseClass. So how do I make sure that SubClass really is serializable.

I took your suggestion and tried with following classes:


public interface TempI {
public void tempMethod();

}

<B>
// I made BaseClass abstract so compiler did not complain even if I didn't
// have implementaion of tempMethod()
</B>

public abstract class BaseClass implements TempI {

}

<B>
//If I remove tempMethod() from SubClass, compiler complains
// so I know for sure that it is indeed implementing
// interface TempI
</B>

public class SubClass extends BaseClass {


/* (non-Javadoc)
* @see testTemp.TempI#tempMethod()
*/
public void tempMethod() {
// TODO Auto-generated method stub

}

Thanks,
P Ingle
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Thanks for the suggestion. My qusetion was related to serializable (or similar) interface that does not have any method of it's own. I have BaseClass inplementing Serializable and SubClass extending BaseClass. So how do I make sure that SubClass really is serializable.

Serializable is a 'tag' interface; it doesn't contain any methods, it is just used to indicate to the system that this class should be Serializable. However it behaves in exactly the same way as other interfaces, so your subclass will indeed be Serializable if the base class is.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
Originally posted by marc weber:


Cool, it made it explicitly prooved to me that the subclass does implement serializable - Thanks,
P Ingle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheriting interfaces