2

Can a Java class subclass both InputStream and OutputStream?

I would like to be able to extend both of these classes if it is possible.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
  • Nope. It is not possible. If you want to provide single interface for both operations in your code, then write a utility which depends on both of these streams and provide the API to user. – K139 May 01 '15 at 20:59
  • As an example of a workaround, java.net.Socket has a getInputStream() method and getOutputStream() method. – Nayuki May 01 '15 at 21:16
  • Sadly InputStream and OutputStream are not interfaces – Steve Kuo May 01 '15 at 22:44

3 Answers3

5

Both InputStream and OutputStream are classes (not interfaces). Java doesn't support multiple inheritance, so it is not possible to subclass them directly.

However, by using composition you can have behaviours from these two classes in one class.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
4

Java does not support multiple inheritance, so you cannot extend multiple classes. You can, however, implement multiple interfaces.

For what you are trying to do, you want to use composition instead of inheritance. So your class would have an instance of InputStream and OutputStream, and you can delegate behavior to each one depending on what you want to do.

Why composition? Inheritance is usually the first thing people reach for, even though what they want to do is to use the behavior of a parent class. For multiple inheritance, usually they want to have access to behaviors of two or more classes. For example, you want a class C that does the things class A does and the things that class B does.From a type perspective though, inheritance is an "is a" relationship. If you say SubClass extends ParentClass, you are saying that SubClass is essentially ParentClass type-wise, and that you can replace instances of ParentClass with SubClass and your program would still be essentially correct. This is called the Liskov Substitution Principle. You can see that making a class that extends both InputStream and OutputStream is a class that has both behaviors, but not strictly one or the other. It is highly unlikely that you would be able to replace instances of both with an instance of your class and expect things to work "correctly". So you need to think about what you really want to do. If you don't care about the internals of the class that you're thinking of extending, but you simply want to make use of its behavior (or alter its behavior in some way), then you can maintain an internal instance of it and delegate to that, while making any alterations in behavior that you need.

So in your case, what you want is access to the I/O behavior of both classes. Hence, you can just maintain internal instances and then provide an API from your class that delegates to the appropriate methods on both instances. The advantage here is abstraction and encapsulation as well. No one who uses your class and its API has any idea how it is handling I/O internally, and they don't really need to know; it is an implementation detail. Also, if you want to change the internal implementation in the future, you can do so easily since the only interface to your class is through your public API. As long as you don't modify the behavior and violate the contract of your API, you can change the internal implementation however you want.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
3

It is not possible as multiple inheritance is not supported in Java. You cannot inherit two classes(InputStream and OutputStream) together in a class.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331