[2005] Abstract/Interface/Class?
I need a bit of guidance on the way to implement my project.
I am going to communicate with several manufacturers of an instrument e.g. an LCD Monitor.
Each has the same functions e.g. On/Off, Brightness, Position etc etc
However, the commands for each manufacturer to do these functions is different.
I'm not sure whether I should have a Monitor interface then have other classes that implement the interface, or whether to have an abstract class and inherit that. :ehh: :sick:
Re: [2005] Abstract/Interface/Class?
Depends how much they have in common. If they are very varied, then use an interface. If they share a lot of common code with some specialisations, then use an abstract class.
Re: [2005] Abstract/Interface/Class?
The relationship between a class and an interface is the same between itself and it's parent class. But I'd go with the super class, because I don't think it'll do any good to your classes just inheriting the Object class.
And for the code that penagate mentioned, it doesn't matter. If it's shared write it in the super otherwise give it a name and make it abstract (Must be overridden in each and all sub classes)
Re: [2005] Abstract/Interface/Class?
Think of it this way: a base class is something that a class IS, while an interface is a role that a class can fill. In the real world you have the Printer class. You cannot create a Printer object without it being a specific type of printer, so Printer is an abstract class. Now many printers provide a USB interface, but then so do scanners, digital cameras and God knows how many other incredibly varied peripherals. They don't inherit the Printer class but they all provide a USB interface, so they can all be plugged into a computer's USB port. Remember that OOP has been designed to mimic real life.
Re: [2005] Abstract/Interface/Class?
Quote:
Originally Posted by jmcilhinney
Think of it this way: a base class is something that a class IS, while an interface is a role that a class can fill. In the real world you have the Printer class. You cannot create a Printer object without it being a specific type of printer, so Printer is an abstract class. Now many printers provide a USB interface, but then so do scanners, digital cameras and God knows how many other incredibly varied peripherals. They don't inherit the Printer class but they all provide a USB interface, so they can all be plugged into a computer's USB port. Remember that OOP has been designed to mimic real life.
I'm not one to judge, but every time I read jmcilhinney's responses I feel like I've learned to look at something in a different perspective. I'm fully aware of how OOP, and I definately know when or when not to interface, inherit, polymorph and all those cool buzz words :). However, I think when jmcilhinney (God i wish I knew your first name cause typing that is not fun) explains something it has a great meaning. To that I say GREAT EXPLANATION! :afrog:
Re: [2005] Abstract/Interface/Class?
I agree that typing JMs name is not fun, however, I found a way around it ;)