Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputstream is an abstract class so we cannot instantiate it.Why then a default constructor is provided for Outputstream class?

Tags:

java

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

like image 975
user3790666 Avatar asked Aug 01 '14 13:08

user3790666


2 Answers

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.

like image 132
Jon Skeet Avatar answered Sep 30 '22 21:09

Jon Skeet


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.

like image 28
Jack Avatar answered Sep 30 '22 21:09

Jack