C# program to demonstrate the example of a sealed class

Sealed class in C#: Here, we are going to learn about the sealed class and its C# implementation.
Submitted by Nidhi, on August 18, 2020

Here we will demonstrate the example of a sealed class. As we know that if we create a class as a sealed class then it is restricted that we cannot inherit that class into another class.

Program:

The source code to demonstrate the example of a sealed class in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// Program to demonstrate the 
// example of a sealed class in C#.

using System;

sealed class MySealedClass
{
    public int num1;
    public int num2;

    public MySealedClass()
    {
        num1 = 500;
        num2 = 800;
    }
}

class SealedTest
{
    static void Main()
    {
        MySealedClass OB = new MySealedClass();
        
        Console.WriteLine(OB.num1);
        Console.WriteLine(OB.num2);
    }
}

Output:

500
800
Press any key to continue . . .

Explanation:

In the above program, we created a sealed class MySealedClass that contains two public data members initialized with 500 and 800 in the constructor of the same class.

We created one more class SealedTest that contains Main() method, in the Main() method we created object OB of MySealedClass and print the value of data members on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.