• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheriting interfaces

 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:


Cool, it made it explicitly prooved to me that the subclass does implement serializable - Thanks,
P Ingle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic