Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been running into this problem more and more: I am not happy with the current API, and end up making my own class that does what I wish the API did; however, I don't extend the 'super class' as I don't feel it fully fits. I can't imagine that this is good practice, so should I just extend the 'super class' or should I name the class slightly different than what I feel the original class should have done?

For example, in Java you end up writing ten lines of code just to read a file into a List, with each entry being one of the file.

So again, in cases like this, should the class be renamed to something similar, or should it go ahead and just extend the class the include the behavior you wanted? It almost seems wrong to me to extend the API classes, but at the same time, by not doing so, you are defeating the point of inheritance in OOP.

share|improve this question
    
1) "don't extend the 'super class' as I don't feel it fully fits. I can't imagine that this is good practice" - per my experience, in about 90% cases it is a good practice. 2) "defeating the point of inheritance" there's no point nor defeating. 3) in rare cases when I need inheritance it's bloody obvious that it's needed; if you can't feel that, use code review to find out –  gnat Apr 23 '12 at 4:36

2 Answers 2

up vote 3 down vote accepted

You shouldn't inherit from File, or any other class to add additional features to it. Your new class is not a File, but rather a set of utility methods to the class File. In other words, there wouldn't be any methods you could pass in NewSuperFile into that would make that method behave differently.

In C# you can create Extension methods. So in this case you wouldn't need to inherit from a class to add additional methods to it, syntactically.

In Java the typical paradigm is to create a ClassUtils class. In this instance you should create either an IOUtils, or FileUtils.

public class FileUtils {         
    /**
     * Read the file one line at a time, and return an array of lines
     * @return List<String>
     */
    public static List<String> readFile(File file) throws IOException {     
            List<String> lines = new ArrayList<>();
            try (InputStream in = Files.newInputStream(file);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } 
        return lines;
    }   
}
share|improve this answer

I would add your new helper-methods to the superclass only if

  • it does not introduce new dependencies (i.e. an additional lib/dll is required)
  • does not change the semantic of existing methods in that superclass: Liskov substitution principle

else i would create a new class.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.