Abstract class and C#properties?

Asked

Viewed 310 times

4

Hello, everyone!

Next, I created an abstract class called Tables, where classes of basic tables of the system will inherit from this abstract class. As 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 happens that, for example, the Category class that inherits from the abstract, does not contain the Fields Idestado and Idcidade. But when instantiating it, the object references (accesses) these two properties. The same happens with Classe Bairro, where it is to access only Idcidade, access also Idestado. How to implement this more performatically? I have to create two new abstract classes with Idestado and Idcidade respectively, or not define these two attributes in the abstract class, but define only in the derived class?

I’m sorry if I’ve been confused.

Thank you.

  • 2

    Yeah, you’re confused. Maybe that’s why you’re having trouble understanding. Rethink and try writing in a less confusing way. Then who knows will even understand. The way it is difficult to answer something, would have to guess what you want. Reread the written text. You may have noticed that it does not make much sense what is written. If you tidy up, it is easier to help.

  • 1

    The abstract class should only have methods and properties that should exist in all derivatives. When creating derivatives add those related to it.

  • I even wonder if you really need an abstract class. As for the fields, inheritance does this, you need to review your modeling concepts for the project by example, it seems to me that you have several objects that need to be identified by type and instance in the system, something like a basic class for everyone (like Object). That’s it?

1 answer

4

You’re experiencing a very common problem that happens when we see in inheritance a solution for code reuse: it is difficult to find in the hierarchical chain the right class to publish an attribute or behavior and, because we do not find the ideal class, the member ends up appearing where he should and also where he should not.

Inheritance is applicable to obtain polymorphism in a hierarchical relation. If the relationship is not hierarchical, using interfaces may be more appropriate (and is 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 right modeling, dig deeper into the problem. What problem do you want to solve with this hierarchy? What are all the entities involved?

Also consider whether you really need the objects in the string to take the form of each other (polymorphism) or if you’re just trying to reuse code (or worse: reuse only member statements).

By the entities listed in the question, one can conclude that what you need is to represent each of these entities in their most specific form ("Category", "State", "City" and "Neighborhood"), and also needs to 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.