5

This question is a following question of null source for FilterInputStream/FilterOutputStream

This question may be a duplicated with protected vs public constructor for abstract class? Is there a difference? (C#)

I found that FilterInputStream designed like this.

public class FilterInputStream extends InputStream { // concrete

    protected FilterInputStream(InputStream in) { // protected
        // ...
    }
}

My question is, is there any difference if the code were

public abstract class FilterInputStream extends InputStream { // abstract

    public FilterInputStream(InputStream in) { // public
        // ...
    }
}
Community
  • 1
  • 1
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

2 Answers2

3

The main difference is that The first class can be instanciated whereas the second class cannot be instanciated because it is abstract.

A protected constructor can be called by subclasses or by classes in the same package.

Therefore any class in the package java.io can call new FilterInputStream() whereas classes in other packages cannot do this.

Also the question is if there are possible other constructors in the class for the first case?

So from inheritance there is not real difference but for using the class from the same package.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
2

Yes There is one main difference that in first code you can use

FilterInputStream fis =new FilterInputStream(in);

but in second code you cannot use

FilterInputStream fis =new FilterInputStream(in);

because abstract class could not have any object. it only be inherited & its child can be instantiated.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
Pulah Nandha
  • 774
  • 4
  • 23
  • 1
    In the first case you can create the instance using constructor only if you are child class or in SAME package. No other class can create the instance. – Narendra Pathai Aug 27 '13 at 06:07