Sealed Classes

 The sealed classes can be declared using “sealed” keyword as follows:
sealed class classname
{
//some members
}
 The sealed classes are just like normal classes, but you can’t inherit the sealed class. In other words, you can’t create a sub class for the sealed class.
 Trying to inherit the sealed class would cause a compilation error.
 This is just like “Final classes” in java.
 Demo:
sealed class one
{

}
class two : one //can’t inherit this. Will give a compilation error.
{

}
Summary of Modifiers

 Up to now, you learned several modifiers like static, new, virtual, abstract, override, sealed.
 You get a summary of all of these modifiers with following table:

Sl. No Modifier Applies to Description
1 new Methods only The method hides a base class method with same signature.
2 static All members The member does not depends / instantiates with any of with its class’s objects. It can be accessed with its class name.
3 virtual Methods only The member can be overridden by a derived class.
4 abstract Classes and Methods only The abstract class can’t be instantiated. The abstract method is nothing but a virtual method that contains only declaration with no definition. It should be implemented with “override” keyword in the derived class.
5 override Methods only The member overrides an inherited virtual or abstract member of the base class.
6 sealed Classes only The class can’t be inherited.

Interfaces

 This is the one of the most important features of C#.
 The .NET beginners may not understand the importance of the interfaces exactly, but in the real-time development of .NET applications, the interfaces are used by the Sr. Developers and the Jr. Developers.
 The “Interfaces” are similar to abstract classes, but having its own features along with having some differences with the abstract classes.

Features of Interfaces:

1. The interface can be declared with “interface” keyword, as follows:
interface interfacename
{

}
2. The interface contains only method declarations and can’t contain method definitions (similar to the abstract methods).
3. The interface can’t contain any data members but can contain “automatic properties” (Already you know that the automatic property doesn’t contain definitions for “get” and “set” accessors).
4. Interface methods are by default “public”. You can’t use another access modifier like private and public etc.
5. The interface can’t be instantiated. That means you can’t create an object for the interface.
6. The interface can’t contain constructors.
7. The class that inherits the interface is called as Implementation Class.
8. The implementation class should implement the definitions for all the interface methods. If not, it would generate compile time errors.
9. One interface can be inherited by any no. of classes (Hierarchical inheritance).
10. One class can inherit any no. of interfaces (Multiple inheritance).
11. The interface methods can’t be declared as “virtual” or “static” in the interface definition.


Implementation Syntax of Interfaces:
//interface definition
interface interfacename
{
returntype methodname(arguments);
}

//implementation class definition
class classname : interfacename
{
public returntype methodname(arguments)
{
//some code
}
}

Interfaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterfaceDemo
{
interface SimpleInterest
{
double GetSimpleInterest(double P, int N, double R);
}
class SimpleInterestImpl : SimpleInterest
{
public double GetSimpleInterest(double P, int N, double R)
{
return ((P * N * R) / 100);
}
}
class Program
{
static void Main(string[] args)
{
SimpleInterestImpl sii = new SimpleInterestImpl();
Console.WriteLine("Simple Interest: " + sii.GetSimpleInterest(5000, 3, 4.5));
Console.Read();
}
}
}
sealed class