views:

101

answers:

3

Hi, I have this scenario:

This two clases with this attributes:

  • Table [Id, Name, Parent_Id]
  • Field [Id, Name, Creation_Time, Creation_Date]

Is it a good practice to make an Abstract Class to implemet the attributes and properties for ID and NAME so that I save some time writting code; or is it better to rewrite each attribute and property on each class? Or maybe use an Interface?

Thanks.

+1  A: 

I don't think either choice make sense. If you use an abstract class, when someone looks at a class that inherits from your abstract class they may be at pains to discover just where that information is. It's probably the worst part about inheritance: hiding important details in a class up in the inheritance chain somewhere.

An interface is kind of a contract that a class has to adhere to so that, usually, it can be used by some other mechanism. A great example is an iterator, such a class usually needs a "next" method. You guarantee that an instance of a class can be iterated if it subscribes to an interface which requires it to have a "next" method. That way a mechanism that wants to iterate through your instance can know it will work. If that's not making sense for what you want to do, maybe don't use this.

apphacker
Thanks for your answer, I'm agreed with you 100%. I was not sure about my decision, that's why I posted here. Thanks.
lidermin
A: 

Subclasses should have an "is a" relationship with their parent class. Table and field seem so different entities that inheritance should be avoided. But on the other hand, they could have a shared base class like DatabaseObject (or something like that) which implements the common attributes. But if this seems too artificial, I would avoid inheritance in these simple cases.

Lauri
Hi, I'm completely agreed with you, this problem is from my job, and after searching on the web, I didn't find the correct answer, so that's why I posted here. I think same way as you, avoid inheritance. Thanks.
lidermin
A: 

If you want to solve a problem of code repitition of couse you can use it.

But if you can use it, from the logic side, you must undestand that all classes extends this abstarct class must have smth common.

den bardadym