Case of using inheritance or interface in Java?

I have a very simple application that needs a data source. This is currently a flat file, but later it will change to a database. I need your opinion on using inheritance or interfaces. Which one will be better and why? My opinion is to use interfaces because it would be flexible, but then I could make the data source also an abstract class.

+5
source share
6 answers

You should use an abstract class where there is common functionality that all implementations should have access to. Here the databases and file implementations are so different that they will have nothing to do, so I would go with the interfaces.

+8

, . , , , /​​.

, , . , , .

+2

.

, , . , , - , .

0

. , , , , .

, , , - int .

0

, , , . , .

To work with a flat file, you can implement a class FileDataWrapper implements DataWrapperwhere the FileDataWrapper class implements operations that can be performed in the data source. Later, when you go to the database, just implement the class DatabaseDataWrapper implements DataWrapper.

0
source

You can use the DAO and Factory design pattern. You can find an example in Advanced DAO programming and more details in Basic J2EE Templates - Data Access Object .

0
source

All Articles