To overcome the drawbacks of procedural languages, the object-oriented language is introduced and
this provides additional benefits like the security and reusability of code.
Eg: C++, C#, Java, etc.
So in the object-oriented programming language, the members like variables and functions are kept under a
container known as a class. This class provides security for the content of the class.
The Class is a user-defined data type similar to the structure in procedural languages.
The structure can contain only variables in it whereas a Class can contain both variables and functions.
A copy of a complex type like class or structure is called an Object.
A data type cannot be used directly, if we want to use then we need to create a copy of it.
int = 5; // This is invalid.
int p = 5; // This is valid.
Similarly, any user-defined type like class or structure cannot be used directly, we need to create a copy
of that class which is an object, and consume the members of the class with the object.
Class MySample
{
//Function members
}
Main Method()
{
//Create object of class MySample
//Access the members of the class using the object.
}
A Class that is declared by using a static modifier is known as a static class. A static class can contain only
static members in it.
We cannot create an object to a static class because it contains only static members. These static methods
are accessed by using the class name.
Syntax:
Example:
The output will be:
The class which cannot be extended further is called Sealed Class. These classes use the sealed modifier.
So the sealed classes cannot participate in inheritance.
Syntax:
Example:
The output will be: