10

here is the link for API documentation of Outputstream abstract class .You will find a default constructor

http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#OutputStream%28%29

3
  • Add more description about Question. Aug 1, 2014 at 13:52
  • Well subclass constructors will have to chain to something... (It could be made protected, admittedly.)
    – Jon Skeet
    Aug 1, 2014 at 13:52
  • And no, that isn't a link for the API documentation of OutputStream...
    – Jon Skeet
    Aug 1, 2014 at 13:53

4 Answers 4

6

The class has to have at least one constructor, because all Java classes have constructors. Additionally, subclasses will have to chain to it - so it's got to be at least protected accessibility. The constructor doesn't need to do anything, so the authors decided not to provide an explicit one. Now, from JLS 8.8.9:

The default constructor has the same accessibility as the class

That's why it's public. It could have been explicitly provided as:

protected OutputStream() {
}

... or better yet, the JLS could have made it so that public abstract class default constructors were implicitly protected. However, it does no harm for it to be public.

1
  • 1
    Basically, it is a harmless API design error resulting from an oversight.
    – usr
    Aug 1, 2014 at 17:37
1

The default constructor is always present in every java class, if there is no other constructor defined. This makes sense, since you need to have some way to instantiate the class. For abstract classes there still needs to be a constructor that can be called by the constructors of the sub-classes. Even if you do not explicitly write a super() statement as the first statement of a constructor, it is implicitly added by the compiler and executed at runtime.

0

Every class in Java must have a constructor. What you say I understand is that when I cannot create its object what's the use of constructor ?? But its the rule and any subclass when created calls the base class constructor like a chain process. It could have been made protected

0

I think the question is very relevant. The no-argument constructor is implicitly available if no constructor is explicitly defined.

Since the abstract OutputStream class has no other constructors defined, the no-arg constructor is not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.