Nathan Campos
Nathan Campos

Reputation: 29497

Having Problems On Organizing Code In Packages

On my Android project I have the main Activity called TestFIO, which is in the package org.testing.file.io.main, and I tried to keep it clear and sent all the functions I had to a new class called FileManipulator, which is located at org.testing.file.io.main.manipulator. Here is how the FileManipulator class looks like:

package org.testing.file.io.main.manipulator;

// imports here

public class FileManipulator extends TestFIO {
    public String readFileFromCard(String location) {
        // some code here
    }

    // more functions here
}

And here is an example of TestFIO:

// header with package and imports

import org.testing.file.io.main.manipulator.FileManipulator;

public class TestFIO extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String[] fileString = readFileFromCard(Environment.getExternalStorageDirectory() + "test.txt");
    }
}

The problem is that Eclipse is underlining readFileFromCard and showing the following error:

Eclipse Error Dialog

What am I doing wrong or how is the correct way to organize my code in packages?

PS: Sorry if this is a dumb question, I'm coming from iOS development.

Upvotes: 1

Views: 89

Answers (2)

E-Riz
E-Riz

Reputation: 32914

The compile problem is because you're trying to call a method defined in a subclass from the superclass. Inheritance doesn't work that way; subclasses inherit all public and protected methods from the superclass, but superclasses don't know anything about the methods of their subclasses.

Additionally, it doesn't seem reasonable to have FileManipulator extend your Activity class. Does FileManipulator pass the "is-a" test, in other words, is it a kind of Activity? It seems more like it's a "helper" class that the Activity will use to do its work. In that case, FileManipulator should not extend TestFIO but rather be stand-alone, created by TestFIO.

Upvotes: 1

lulumeya
lulumeya

Reputation: 1628

I see TestFIO is parent class, FileManipulator is child class according to your code. Then you can't call child class' method, you need a instance of FileManipulator. Am I wrong?

Upvotes: 1

Related Questions