Class . It is a construction that allows you to create your own custom types by grouping variables of other types, methods and events. A class is like a plane. Defines the data and behavior of a type. If the class is not declared as static, the client code can use it by creating objects or instances that are assigned to a variable. The variable remains in memory until all references to it are out of scope. If the class is declared static, there is only one copy in memory and the client code can only access it through the class itself and not through an instance variable.
Summary
[ hide ]
- 1 Types of classes
- 2 Fundamental characteristics
- 3 Declaration
- 4 Inheritance
- 5 Example
- 6 Sources
Types of classes
A very important part of object-oriented programming is classes, if it weren’t for them there wouldn’t even be objects. Each one has its own characteristics and advantages. A programmer who knows these characteristics knows when to use one and not the other, so that he develops good software. The types of classes are:
- Public : They are very common, accessible from any other class in the same library (otherwise they have to be imported).
- Abstract : Those that have at least one abstract method. They do not implement their methods , but rather provide the basis for them to be implemented in inheritance.
- End : They are the ones that end the chain of inheritance. Useful for security and efficiency reasons of a program, since they do not allow to create more sub-divisions below this class.
- Synchronizable : Specifies that its methods are synchronized, avoiding problems with threads, so that they cannot start running one method if the other has not finished.
Key Features
- Class name. It is used to identify all objects that have certain characteristics.
- Attribute set. Member data. The value of the attributes represent the state of each object.
- Set of methods. Member functions . Allows objects to change state, depending on the previous state the object had.
- Access levelsto protect certain class members. Typically, attributes will be defined as hidden (private) and methods visible (public).
Statement
Classes are declared using the class keyword. The access level precedes the class keyword. Since public is used in this case, anyone can create objects from this class. The class name follows the keyword class. The rest of the definition is the class body, where the behavior and data are defined. The fields, properties, methods, and events of a class are collectively known as class members. Although sometimes used interchangeably, a class and an object are different things. A class defines a type of object, but it is not properly an object. An object is a specific entity based on a class and is sometimes called an instance of a class.
Heritage
Inheritance is done through a derivation, which means that a class is declared using a base class from which it inherits the data and behavior. A base class is specified by appending a colon and the name of the base class. When a class declares a base class, it inherits all the members of the base class except the constructors. Unlike C ++, a class in C # can only inherit directly from a base class. However, since a base class can inherit from another class, a class can indirectly inherit from several base classes. Also, a class can directly implement more than one interface. A class can be declared as abstract. An abstract class contains abstract methods that include a signature definition but no implementation. Cannot instantiate abstract classes. They can only be used through derived classes that implement abstract methods . Conversely, a sealed class does not allow other classes to derive from it.
