Reasons to use private class

Asked

Viewed 1,859 times

13

When I started in the area, in a course of POO (Object Oriented Programming) the teacher explained about access modifiers...

I remember that on this very day he said that it would be possible to create a private class, but he saw no reason for it.

Why is it possible to declare classes private? Can I use them somehow? If so, how?

Obs.: I ask with no intention of breadth. If you consider too broad, please give me examples...

  • 2

    I feel like the answers to that will be based primarily on opinion, like your naturalist professor, but I’m going to participate =)

  • @Diegorafaelsouza? Hope to know some development patterns that I have not seen yet and that can use this way... Anyway... Let’s go :)

  • 1
  • 1

    The question should not focus on other languages, only on C# since they have already positioned as duplicate in C# has some differences

  • So @Rovannlinhalis, there is treated the access modifier. Here I’m asking more about the same about a class. What’s different, no?

  • Yeah. In fact. It’s marked as java, but it really all applies to the general concept of OO. Specifically this answer I think it says what you want to know.

  • Good @Virgilionovic, edited. Thanks!

  • @Diegorafaelsouza, actually, from what I understand, they talk about attributes and methods. I believe that classes have different treatments, don’t you? After all, how to access it? Not a class in the same namespace could use it... I mean, it would be useless?

  • 1

    As @Diegorafaelsouza said, despite being in java, the concept of OO is the same... if your teacher only makes small projects, should not even make a difference to him... But basically you use the private whenever that resource (class, variable, method, etc.) is only available in the class itself. There are some security factors to work like this, (including to protect the program of the own programmer rsrs) besides being very useful for organizing the codes / projects within a very large Solution

  • 1

    Really, the concept of OO is the same. But you see, creating a private class is different than creating a method or a private attribute. If you create a private class, who will use it and how? That I don’t understand...

  • 1

    you can create a class within another... within a control, within a Form... in which case it would be restricted to the Higher class. Here is a simple example: https://dotnetfiddle.net/9jcmrl

  • 1

    @Rovannlinhalis, now things are starting to make more sense. Are there other examples, perhaps patterns? If you can elaborate an answer to that I thank you

  • @Rovannlinhalis, I confess that I found very strange this concept, but it really works...

  • 1

    Once I get home, if not an answer that clarifies your doubt, I’ll try to do it. But it is quite possible that soon there will appear here a reply from Mario explaining everything perfectly rsrs

  • 1

    Thanks Dude. huiahauhau Let’s see :)

Show 10 more comments

1 answer

18


Private class is very little useful because it is available only within another class, that is, only the external class can instantiate an object of the inner class that is private. Not even returning an object of this type outside the class is possible because the type does not exist outside the class.

When the type is really only useful internally there is no reason to let the class be accessible from outside. The smaller the scope, the less errors you may have in your code, the less pollution namespace will have, so it is easier to locate what you want.

There is little reason, but there are cases for its use. This is done to hide implementation details. So one day you can change whatever you want without worrying. Everything that is exposed out of class becomes a contract that you have to respect under penalty of breaking existing codes.

A simplified and unnecessary example in this context, just to illustrate:

public class Classe {
    Temp temp = new Temp();
    public string Texto { get => temp.Str; set => temp.Str = value; }
    public int Valor { get => temp.I; set => temp.I = value; }

    private class Temp {
        public string Str { get; set; }
        public int I { get; set; }
    }
}

I put in the Github for future reference.

You cannot create an object of the type Temp out of this class.

One thing few people know is that the standard of the class is internal, that is, it is only accessible within the file itself Assembly code. To make the class accessible to every application you need to use the attribute public in it.

In other languages an internal class can have other functions, and if you try to learn from them, you won’t be learning how it works in C# which is just that.

  • 1

    I tried to respond by exemplifying the implementation of a Driver for the Database. For example, we would have a class Conexao, and we could have a private class to implement some feature for example a Buffer to store records and pick up more records when Buffer closes. That would be a case for your answer?

  • 2

    @lemoce would most likely be a valid example, of course, depending on the implementation and need.

  • An analogy: A processor has a logic unit internally, it is completely private to the processor, it is not possible to get it from the processor nor change it (although it may find something similar in the market), but it is possible to use itI was going through some processor interface. In the case of programming it is only a logical encapsulation.

Browser other questions tagged

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