Re: extending abstract class
- From: "Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx>
- Date: 30 Jan 2006 12:17:57 +0100
"Carramba" wrote
> if answer few (well many) question? so I one day can stop asking :P
>
> import java.io.*;
> import java.util.*;
>
> abstract class FileCommand {
>
> protected String fileName; //why should I have declare it here
> //and the assign fileName = fn;
> //could I just use fn direct?
That depends on how you'll be using that information further on. If you at
some point want to get it in another method, you'll need a reference to it.
Otherwise not.
> public ArrayList<Integer> fileData = new ArrayList<Integer>();
> //<-could I still use it in ByteChar is it was private?
If you declare it private it will not be visible for the subclasses.
> public FileCommand(String fn) { //can abstract class throw extension?
I guess you mean "Exception".
Yes, a method/constructor in an abstract class can be declared to throw
Exceptions.
> fileName = fn;
>
> FileInputStream fis; //should thouse 2 by private?
> BufferedInputStream bis;
Visibility modifiers are not applicable to local variables. They will always
only be visible from within the method anyway.
> try {
> fis = new FileInputStream(fileName);
> bis = new BufferedInputStream(fis);
> while(true) {
> int character = bis.read();
> if(character < 0) //is thre any other more smooth methods to
> abort reading?
> break;
> fileData.add(character); //Are integers wraped in there as
> elemnts?
Yes, from JDK 1.5 they're boxed into an Integer object.
> }
> } catch(IOException e) {
> e.printStackTrace(); //
> }
> }
> }
>
>
> public class ByteChar extends FileCommand {
>
> public ByteChar(String fn) {
> super(fn); //<- this means that I create object ByteChar that have
> FileComman propertys?
Nope, it means that this constructor will call the constructor of the
superclass.
However, *as* a subclass of FileCommand, ByteChar will inherit the
properties of FileCommand.
> }
>
> public static void main(String[] args) {
> if(fn == null || fn == "") { //why this doesn't work to catch
> empty file name?
At this time, your code won't even compile, as you doesn't have any field or
variable named "fn"!
Even if you had, you shouldn't compare an object to a literal value in this
way. As Strings are object a direct comparison compares on a reference
level, i.e. if it's the *same* object, not if they're "equal".
That kind of comparison for string values should rather look like this:
if(fn == null || fn.equals("") )
> System.out.println("Please enter File Name");
> }
> String fn = args[0]; //what about relativ patch? shoul one by
> conserned about it?
That depends on your need in the application. Sometimes you need a relative
path, sometimes an absolute...
> ByteChar fd = new ByteChar(fn); //<-but if I have created it with super
> what Iam doing here?
Nope, *this* is the point where the object is created.
In the constructor you define what will happen inside the object *upon*
creation, but the constructor itself doesn't "create" the object.
// Bjorn A
.
- References:
- extending abstract class
- From: Carramba
- Re: extending abstract class
- From: Carramba
- Re: extending abstract class
- From: Andrew McDonagh
- Re: extending abstract class
- From: Carramba
- extending abstract class
- Prev by Date: Re: extending abstract class
- Next by Date: Re: ObjectInputStream, how to find the end of the stream?
- Previous by thread: Re: extending abstract class
- Next by thread: Re: extending abstract class
- Index(es):
Relevant Pages
|