I was able to pass the task, but I still cannot understand why we need to call the super(fileName) in the constructor. Reading this link gave me some more understanding, and in this case the FileOutputStream class obviously doesn't have a constructor that matches what we need.
But it's still not clear for me. Can someone give me a more digested explanation?
TIA
Why do we need to call the superclass constructor with the fileName?
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Johannes
9 April 2020, 06:04
Why do we care to pass a filename to the BASE constructor (the FileOutputStream) ?
We have "copied" it (or overriddden, or implemented, whatever).
So we're not planning to use FileOutputStream anymore, we wanted more ;)
So we created AmigoOutputStream as a "copy" (extended/copied FileOutputStream) to create the same and more functionality. Now we can use the AmigoOutputStream overridden methods, instead of original FileOutputStream, and don't give a hoot any longer about what the base class was doing, or wanted ?
My understanding surely is still very amateur, please comment ?
0
Anthony Chalk
19 February 2020, 10:24
The keyword super is used to invoke an overridden method (or constructor in this case).
Over here, you need to initialise the the FileOutputStream field when using the class's constructor.
The problem is, all the FileOutputStream constructors either take a File or a String with a file name as an argument; the AmigoOutputStream constructor only takes a FileOutputStream Object as an argument.
So, how do we initialise the FileOutputStream field?
We invoke the FileOutputStream class's constructor, and pass fileName as an argument
0
Facundo Villarroel
20 February 2020, 12:18
I still don't see the purpose, I guess I'm missing something.
In the main method of the AmigoOutputStream class we have:
So, we are passing the fileName to the FileOutputStream constructor. All good so far, we create a new FileOutputStream with fileName and pass that object to the AmigoOutputStream constructor. So then, why do we call super(fileName) if we already have created a new FileOutputStream?
Does my questions makes sense? 0
Anthony Chalk
20 February 2020, 13:03
From the article I linked:
"Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error."
As FileOutputStream doesn't have a no-argument constructor, it would cause an error
+4
klesk
27 March 2020, 11:12
I hope somebody will correct me if i am wrong, but i will try explain to put it in my own words. When you extend the FileOutputStream class, the first think that happens when an object of the class which inherits is initialized, the BASE class constructor is called. In this case the base-class constructor MUST HAVE a parameter because there is no default constructor without parameter. Therefore the parameter we are passing to AmigoOutputStream doesn't matter at this stage.
0
Sela
30 August 2020, 08:15
Anthony Chalk's (user 10482029) answer is the solution to this question
+1