• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

constructors

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, folks:
I am a little bit confused about the invocaiton of superclass constructors in subclass. Look at the following code:
Class A {
public A(){}
public A(int i) {}
}
class B extends A {
public B(){
super();
}
public B(String str, int i) {
super(i);
}
}

This code should compile, I think. Then am I right that:
1. you have to invoke superclass constructor via super() unless super class has no explicitly declared constructor;
2. To invoke a superclass constructor with a signature, the parameters in the signature must be declared in the subclass constructor signature??
If I am right, then I have some problem understanding FileReader class in IO. As FileReader extends InputStreamReader and they have the following constructors:
class InputStreamReader {
public InputStreamReader(InputStream in) {}
public InputStreamReader(InputStream in, String enc) {}
}
class FileReader extends InputStreamReader {
public FileReader(File file) {}
public FileReader(String path) {}
public FileReader(FileDescriptor fdObj) {}
}
As no InputStream is declared in FileReader constructor, how can InputStreamReader constructor be invoked in FileReader constructor? Another question is, which InputStreamReader constructor was actually invoked in any of these three FileReader constructor? Can you invoke all three Constructors? (but you can not have more than one super(), right?).
Now, for practical reason, I want to declare a FileReader object to read character from a file. But I want to specify the character encoding. I know you can only specify character encoding with InputStreamReader as it is the bridge between byte and character. As FileReader is a subclass of InputStreamReader, it should be able to do so. Now, how can I do it?
Thanks in advance
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Weigang,
Compiling your code I get the following:
1)"FileReader.java": Error #: 300 : class InputStream not found in class InputStreamReader at line 4, column 26
2)"FileReader.java": Error #: 300 : class InputStream not found in class InputStreamReader at line 5, column 26
3)"FileReader.java": Error #: 300 : class File not found in class FileReader at line 9, column 19
4)"FileReader.java": Error #: 300 : class FileDescriptor not found in class FileReader at line 11, column 19
5)"FileReader.java": Error #: 300 : constructor InputStreamReader() not found in class InputStreamReader at line 9, column 8
6)"FileReader.java": Error #: 300 : constructor InputStreamReader() not found in class InputStreamReader at line 10, column 8
7)"FileReader.java": Error #: 300 : constructor InputStreamReader() not found in class InputStreamReader at line 11, column 8
Here's my take on the reasons:
Errors 1-4 are caused by not having the import statement:
import java.io.* or if you want to generate your own you need to import whatever package they are in.Obviously, the compiler needs to know where these files came from.
Errors 5-7 are caused by not having the constructor InputStreamReader() implicitly specified. I forget the specific rule but I'll try and look it up when I find more time.
Here's some code to read chars:
<code>
BufferedReader d = new BufferedReader(new FileReader(file));
// Continue to read lines while
// there are still some left to read
while ((line = d.readLine()) != null) {
// Print file line to screen
buffer.append( indenter( line.trim()) );
}
</code>
You're right there is a way to specify the type of character encoding. FileReader is a "Convenience class for reading character files." - from Java API specs. To specify a particular encoding format I'm going to have to refer you to the Java API for InputStreamReader which contains some links to help you out. But why would you want to specify an encoding other than byte or char anyways?
Anyone, please correct me if I'm misleading Weigang.
Hope this helps.
cheers,
Yoo-Jin
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic