Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance of classes that aren't related in the traditional sense of OOP

I am wondering if I am approaching my project the right way in terms of OOP. My project consists of a windows console application in c#. The point of the application is to take user input, convert it to an sql query and query the connecting database and then spit out the information in a reader-friendly format that the user requested. I have the following 3 classes right now:

Class: commandLineInterpreter extends sqlQueries

this class prints out the commands that the user can utilize. it also takes the user input.

Class: sqlQueries extends dbConnect

this class holds the sql queries that will query the database depending on what the user is inputing.

Class: dbConnect this class initlizes the database connection and prints out a message saying if it suceeded or not.

as you can see i have the commandlineInterpreter class that extends the sql query class that then extends the db connect class.

When I initlize the commandline class in the main() function, it automatically intitlizes the other extend classes as well. I did this because without connecting to the DB then the commandLine interpreter is useless as it cant provide any answers.

My question is, even though these classes aren't related in an inheritance sense of OOP, does it still make sense to do class inheritance this way? or is there a better way to organize my code?

like image 715
john Avatar asked Dec 19 '22 10:12

john


1 Answers

I slightly disagree with this design as it seems your objects have no relational hierarchy.

The command line interpreter should not know anything about the SQL Queries class. It's kind of a separation of concern and it's only job is to read user input, with the collected input you can then simply pass it to the next service layer, i.e. the SQL Queries class.

like image 68
Darren Avatar answered Dec 21 '22 22:12

Darren