Class and Object

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.

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.

Object:

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.
}

 

Static Class:

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: 

static class XX
{


}

 

Example:

 

using System;
namespace SampleProgram {
  static class MySample {
    public static string greet = “Hello World”;
    public static void GreetInfo() {
      Console.WriteLine("Hello !! ");
    }
  }
  class MainClass {
    public static void Main(string[] args) {
      MySample.GreetInfo();
      Console.WriteLine(MySample.greet);
    }
  }
}

 

The output will be:

Hello!!
Hello World

 

Sealed Class:

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:

sealed class XX
{


}

 

Example:

 

using System;
namespace SampleProgram {
  sealed class MySample {
    public string greet = “Hello World”;
    public void GreetInfo() {
      Console.WriteLine( "Hello !! ");
    }
  }
  class MainClass: MySample // Cannot inherit the sealed class
  {
    public static void Main(string[] args) {
}
  }
}

The output will be:

Error CS0509 cannot derive from sealed type ‘MySample

 

 

Related Tutorials