Concept of Class in .NET

Introduction

Class
Class can be defined as a collection of similar kind of objects.
Class can be defined by various ways:

  1. Conceptually 
  2. Programmatically 
Conceptually
  1. It is a classification. 
  2. Customization - creates according to our need.
  3. Aggregation - for various object.
Programmatically
  1. Logical entity - because it doesn't takes any memory. 
  2. Type for Object - We can make object of class types.
  3. Class is a keyword.
  4. Class consists - member function & member variable.
  5. Class keyword knows only two operators

    --Assignment operator (=) 
    --Ampersand (&)
Class is a Template because through this we can makes many objects.Class are also predefined.

Syntax of Class

class ClassName
{
  // member variables ...
  // member function ...
}

In C#, we have many types of classes

  • Abstract class
  • Partial class
  • Sealed class
  • Static class
Abstract class
  1. Abstract class is one of which we can't create a direct object.
  2. We can't instantiate an abstract class.
  3. It consists both concrete or abstract or both method in it.
  4. Abstract keyword is for making a class Abstract.
  5. Used for Memory managenemt.
  6. Abstract classes are the alternatives of interfaces.
Program of Abstract class

using System;
namespace abstractDeep
{
   abstract class Car
    {
       string regno;
       int modelno;
       public int Model // This is property 1st
       {
           set { modelno = value; }
           get { return modelno; }
       }
       public string Resgistration // This is property 2nd
       {
           set { regno = value; }
           get { return regno; }
       }
       public void brakes() // function
       {
           Console.WriteLine("Brakes are important to drive safely");
       }    
    }
  class Maruti:Car
   {  
     public static void Main()
       {
           Maruti mr = new Maruti();
           Console.WriteLine("        Maruti CAR DESCRIPTION");
           Console.WriteLine("*******************************************");
           mr.Model = 2005;
           mr.Resgistration = "UP-14 Z 6224-DEEPAK";
           Console.WriteLine("CarModel        : " + mr.Model + "");
           Console.WriteLine("CarRegistration : " + mr.Resgistration + "");
           mr.brakes();
           Console.WriteLine("*******************************************");
           Console.Read();
       }
  }
}


Output

abstratc.gif

Scenario of Abstract class

If we have to make Cars with some same features then, we make the abstract class then inherit this class with other class of car which can company name (maruti,indica,Tata etc.).Here we can't make the object of Car class but we can make object of maruti cars like Wagon, maruti 800.

Partial class

A class which is distributed over no. of files known as partial class. When we want to create a single class distributed over a number of  files then we use the concept of partial class.

partial - keyword is used for making the class partial.

Syntax of Partial class

public partial class Student
{
    public void DoHomeWork()
    {
    }

public partial class Student
{
    public void GoToLunch()
    {
    }
}


Scenario of Partial class

It is helpful in case when we make a class with many function then no. of person are assigned for that work and they make partial class with same name.
After compilation they can be combined into a single .dll (dynamic link library). 

Sealed class

  1. It is also called LEAF CLASS.
  2. Means which can't be inherited or which can't have child class.
  3. We can only create objects of sealed class instead of inherit it.
Program of Sealed class

using System;
namespace Sealednmsp
{
    class Car
    {
       string regno;
       int modelno;
       public int Model // This is property 1st
       {
           set { modelno = value; }
           get { return modelno; }
       }
       public string Resgistration // This is property 2nd
       {
           set { regno = value; }
           get { return regno; }
       }     
    }
  sealed class WagonR:Car // WagonR is sealed class so u cant inherit class WagonR further
   {  
     public static void Main()
       {
           WagonR mr = new WagonR();
           Console.WriteLine("        WagonR CAR DESCRIPTION");
           Console.WriteLine("****************************************");
           mr.Model = 2005;
           mr.Resgistration ="UP-14 Z 6224-GHAZIABAD";
           Console.WriteLine("CarModel       : " + mr.Model + "");
           Console.WriteLine("CarRegistration: " + mr.Resgistration + "");
           Console.WriteLine("****************************************");
           Console.Read();
       }
   }
  class Final : WagonR  //Error     1      'Sealednmsp.Final': cannot derive from sealed type 'Sealednmsp.WagonR' 
  {
  }
}

Scenario of Sealed class

If we have a car wagon R ,we must make it sealed so that no one can inherit it to use according to his need. One can only have object of Wagon R cars.

Static class

  1. A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
  2. Do not have a callable constructor.
  3. Static class position in memory is therefore fixed in one place.
  4. Static classes are sealed and therefore cannot be inherited.
Syntax of Static class

// static class
static class CollegeStu
{
    public static void DoSomething()
    {
        Console.WriteLine("Be prepare for the exam");
    }
    public static void DoSomethingExtra()
    {
        Console.WriteLine("Being a topper of college need to do hard work with smartness");
    }
}