Abstract class and properties in C #? [closed]

4

Hello, everyone!

Next, I created an abstract class called Tables, where basic system table classes inherit from this abstract class. Like Neighborhood, City, States, category, etc ... The abstract class code looks like this:

public abstract class Tabelas
{

    public int Id { get; set; }
    public string Nome { get; set; }

    public virtual int IdEstado { get; set; }
    public virtual int IdCidade { get; set; }
}

It turns out that, for example, the Category class that inherits from the abstract, does not contain the IdState and IdCity fields. But when instantiating it, the object references (accesses) these two properties. The same happens with the Neighborhood Class, where it is to access only IdCity, access also IdEstado. How to implement this in a more performative way? I need to create two new abstract classes with IdEst and IdCity respectively, or not to define these two attributes in the abstract class, but to define only in the derived class?

Sorry if I was confused.

Thank you.

    
asked by anonymous 04.02.2016 / 23:44

1 answer

4

You're experiencing a very common problem that happens when we see inheritance as a solution for code reuse: It's hard to find the right class to publish an attribute or behavior in the chain and, the ideal class, the member ends up showing up where it should and also where it should not.

Inheritance is applicable to get polymorphism in a hierarchical relationship . If the relationship is not hierarchical, using interfaces may be more appropriate (and often more appropriate even when the relationship is naturally hierarchical).

It is not because an attribute or behavior is useful in more than one object that we should use inheritance to reuse it or "reuse it."

To find the ideal modeling, dig deeper into the problem. What problem do you want to solve with this hierarchy? What are all the entities involved?

Consider also whether you really need the objects in the chain to take the form of each other (polymorphism) or if you are just trying to reuse code (or worse: reuse only member declarations).

By the entities listed in the question, you can conclude that what you need is to represent each of these entities in their most specific form ( "Category" , , and you must also represent all of them in the form of Table .

In this case, this modeling meets and does not suffer from the problem of members appearing where they should not:

interface Tabela
    int Id
    string Nome

class Categoria : Tabela
    int Id // implementação exigida pela interface Tabela
    string Nome // implementação exigida pela interface Tabela

class Estado : Tabela
    int Id // implementação exigida pela interface Tabela
    string Nome // implementação exigida pela interface Tabela

class Cidade: Tabela
    int IdEstado

    int Id // implementação exigida pela interface Tabela
    string Nome // implementação exigida pela interface Tabela

class Bairro : Tabela
    int IdCidade

    int Id // implementação exigida pela interface Tabela
    string Nome // implementação exigida pela interface Tabela
    
05.02.2016 / 16:19