Types Of Class In C#

Class

Class is a blueprint about object that provide an overview of object, as we know that oops consider each and everything as Entity.

Let us consider Person, Person is a class that contain Colors, Height, Age and more as well as Method of Person is Playing, Eating and more.

A person could be me, you and any one which will contain member variable as colors, height, age and method as playing, eating and more.

Syntax for creating class is:

    Class ClassName
    {
       Member variable
       Method()
       {
          Member Decalaration
       }
       Method()
       {
          Implementation
       }
    }

Example

  1. Class Person  
  2. String Name, Color;  
  3. Int Age;  
  4. Public void Get()  
  5. {  
  6.     Console.WriteLine(“Enter Person Details: ”);  
  7.     This.Name = Console.ReadLine();  
  8.     This.Color = Console.ReadLine();  
  9.     This.Age = Convert.ToInt32(Console.ReadLine());  
  10. }  
  11. Public void Display()  
  12. {  
  13.     Console.WriteLine(“Person Name is: ”+this.Name);  
  14.     Console.WriteLine(“Person Color is: ”+this.Color);  
  15.     Console.WriteLine(“Person Age is: ”+this.Age);  
  16. }  
  17. }  
By following this syntax we can write the code for Class.

Object

Object is instance value of Class. Constructor get automatically invoked  when object of class gets created. For creating object, class is mandatory. The syntax for creating object is:

Class Name object=new Class Name()

Example for creating object and implementing it:
  1. Class Person  
  2. String Name, Color;  
  3. Int Age;  
  4. Public void Get()  
  5. {  
  6.     Console.WriteLine(“Enter Person Details: ”);  
  7.     This.Name = Console.ReadLine();  
  8.     This.Color = Console.ReadLine();  
  9.     This.Age = Convert.ToInt32(Console.ReadLine());  
  10. }  
  11. Public void Display()  
  12. {  
  13.     Console.WriteLine(“Person Name is: ”+this.Name);  
  14.     Console.WriteLine(“Person Color is: ”+this.Color);  
  15.     Console.WriteLine(“Person Age is: ”+this.Age);  
  16. }  
  17. }  
  18. Class Person_Details  
  19. Public void Main()  
  20. {  
  21.     Person P1 = new Person();  
  22.     P1.Get();  
  23.     P1.Dispaly();  
  24. }  
  25. Console.Read();  
  26. }  
  27. }  
Types of Class

 

  • Virtual Class: Virtual class is used for implementing new thing on already existing class. We create Virtual class using Virtual keyword, virtual class contain the member and also it implement these. Overriding of Virtual class is not mandatory.

  • Abstract Class: Abstract class is used to avoid rewriting code. By using Abstract keyword we create abstract class, abstract class dos not implement anything but it contain Abstract member. The overriding of Abstract class is mandatory otherwise it will raise error.

    Syntax for Abstract Class
    1. Abstract class ClassName  
    2. Member  
    3. {  
    4.     Abstract Method 1()  
    5.     Abstract Method2()  
    6. }  
    7. Class ChildClassName: Class  
    8. Member  
    9. Public overriding void Method1()  
    10. {  
    11.     console.Write(“Enter Abstract class Details: ”);  
    12.     Base.Member1 = Console.ReadLine();  
    13.     Base.Member2 = Convert.ToInt32(Console.ReadLine());  
    14.     Console.Write(“Enter child class details”);  
    15.     {  
    16.         This.Member3 = Console.ReadLine();  
    17.         This.Member4 = Convert.ToInt32(Console.ReadLine());  
    18.     }  
    19. }  
    20. Public overriding Method2()  
    21. {  
    22.     Console.WriteLine(“”+base.Member1);  
    23.     Console.WriteLine(“”+base.Member2);  
    24.     Console.WriteLine(“”+this.Member3);  
    25.     Console.WriteLine(“”+base.Member4);  
    26. }  
    27. }  
    28. Console.Read();  
    29. }  
    30. Class Details  
    31. Public void Main()  
    32. {  
    33.     ChildClass obj = new ChildClass();  
    34.     Obj.Method1();  
    35.     Obj.Method2();  
    36. }  
  • Sealed Class: Sealed class always be on bottom. We can’t derive class from Sealed class.

  • Partial Class: Dividing source code in more than one part is known as Partial class. In partial class each and every part gets executed individually.

Up Next
    Ebook Download
    View all
    Learn
    View all