Re: extending abstract class




"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


.



Relevant Pages

  • Re: extending abstract class
    ... > public FileCommand{//can abstract class throw extension? ... > public class ByteChar extends FileCommand { ... it means that this constructor will call the constructor of the ... As Strings are object a direct comparison compares on a reference ...
    (comp.lang.java.help)
  • Re: abstract constructor performance?
    ... > the performance hit of the call to the abstract constructor under load. ... my code creates about 200 instances of the subclasses ... If the abstract class has no constructor, then there's no call to it? ... The call itself has a negligible performance penalty - if performance was a ...
    (comp.lang.java.programmer)
  • Re: Factory method
    ... > factory method in my abstract class that creates subclasses. ... > constructor in my subclasses must be able to call the constructor in my ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Factory method
    ... > factory method in my abstract class that creates subclasses. ... > constructor in my subclasses must be able to call the constructor in my ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: extending abstract class
    ... >I want to make abstract class for reading file ... >public abstract class fileCommand { ... >public class fileIO extends fileCommand { ...
    (comp.lang.java.help)