Thursday, December 13, 2012

C# Sealed Clases , Partial Class


Sealed, Partial Classes, Inheritance 

Sealed Classes:

The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override


abstract class ShapesClass
{
    abstract public int Area();
}

class Square : ShapesClass
{
    int x, y;
    // Because ShapesClass.Area is abstract, failing to override
    // the Area method would result in a compilation error.
    public override int Area()
    {
        return x * y;
    }
}

When we create a static class, it becomes automatically sealed. This means that you cannot derive a class from a static class. So, the sealed and the static class have in common that both are sealed. The difference is that you can declared a variable of a sealed class to access its members but you use the name of a static class to access its members.

Partial Classes:

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

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

namespace Csharp
{
    class Inhertiance
    {
        public static void Main(string[] args)
        {
            Reptiles obj = new Reptiles();
            obj.Birds();
            obj.Mammals();
            obj.reptiles();
            Console.ReadLine();
        }
     
    }
    class animals
    {
        public void Mammals()
        {
            Console.WriteLine("The rat belongs to mammals family");
        }
        public void Birds()
        {
            Console.WriteLine("The eagle belongs to birds family");
        }
    }
    class Reptiles : animals
    {
        public void reptiles()
        {
            Console.WriteLine("The snake belongs to reptiles familY");
        }
    }

Sorry guys but i am writing this blog.

No comments:

Post a Comment