Reputation: 39
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.
Upvotes: 2
Views: 1830
Reputation: 95558
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.
Upvotes: 4
Reputation: 172518
It is not possible as multiple inheritance is not supported in Java. You cannot inherit two classes(InputStream and OutputStream) together in a class.
Upvotes: 3
Reputation: 10925
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.
Upvotes: 5