Class, Inheritance And Abstract Class With Real Time Examples In C#

Class: A class is a collection of properties and methods.

Example code

  1. public class cart  
  2.     {  
  3.         int cartID;  
  4.         List<string> list = new List<string>();  
  5.         ArrayList aList = new ArrayList();  
  6.   
  7.         public List<string> getCartDetails(int pcartID)  
  8.         {  
  9.   
  10.             cartID = pcartID;  
  11.   
  12.             list.Add("Product Name");  
  13.             list.Add("Description");  
  14.             list.Add("Size");  
  15.             return list;  
  16.   
  17.         }  
  18.     }  
  19.   
  20. using System;  
  21. using System.Collections.Generic;  
  22. using System.Linq;  
  23. using System.Text;  
  24. using System.Threading.Tasks;  
  25. using System.Data;  
  26. using System.Collections;  
  27.   
  28. namespace ConsoleApplication  
  29. {  
  30.     class Program   
  31.     {  
  32.         static void Main(string[] args)  
  33.         {  
  34.             //Declaration of object  
  35.             cart cObj = new cart();  
  36.   
  37.   
  38.             //Displaying Output for class  
  39.             List<string> range = cObj.getCartDetails(1);  
  40.             foreach (string item in range)  
  41.             {  
  42.                 Console.WriteLine(item);  
  43.             }  
  44.   
  45.             Console.ReadLine();  
  46. }  
  47. }  
  48. }  
Object: An object is an instance of a class.
  1. cart cObj = new cart();  
  2. cObj.getCartDetails(1);   
Inheritance

When a class of data wants to derive another class.

Advantage of inheritance: Reusability of class data.

Example code
  1. public class users  
  2.     {  
  3.         public string userName { getset; }  
  4.         public string passWord { getset; }  
  5.     }  
  6. public class Register : users  
  7.   
  8.     {  
  9.         public int mobileNumber { getset; }  
  10.         public string address { getset; }  
  11.   
  12.           
  13.     }  
  14.   
  15.   
  16. using System;  
  17. using System.Collections.Generic;  
  18. using System.Linq;  
  19. using System.Text;  
  20. using System.Threading.Tasks;  
  21. using System.Data;  
  22. using System.Collections;  
  23.   
  24. namespace ConsoleApplication  
  25. {  
  26.     class Program   
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.               
  31.   
  32.   
  33.              
  34.             Register objUsers = new Register();  
  35.             objUsers.userName = "RajeshGonugunta";  
  36.             objUsers.passWord = "ppppp";  
  37.             objUsers.mobileNumber = 99999;  
  38.             objUsers.address = "Address1";  
  39.   
  40.   
  41.   
  42.   
  43.             List<Register> objUsersList = new List<Register>();  
  44.             objUsersList.Add(objUsers);  
  45.   
  46.             foreach (Register list in objUsersList)  
  47.             {  
  48.                 Console.WriteLine(list.userName);  
  49.                 Console.WriteLine(list.passWord);  
  50.   
  51.                 Console.WriteLine(list.mobileNumber);  
  52.                 Console.WriteLine(list.address);  
  53.             }  
  54. }  
  55. }  
  56. }  
Abstract class

Abstract class is a special class that cannot be instantiated. To get Abstract class, data needs to create a new class and inherit it. Abstract class contains concrete methods and abstract method. Abstract method doesn’t have implementation. To implement, the Abstract method needs to override into another class.
Advantage of abstract class: Avoids the duplication of code/implementation.

Example of code
  1.   public abstract class shipping  
  2.     {  
  3.         public string delivery { getset; }  
  4.         public abstract string shipmentDetails();  
  5.         public bool caluculate()  
  6.         {  
  7.             return true;   
  8.         }  
  9.        
  10.     }  
  11.     public class getShipping :shipping  
  12.     {  
  13.         public override string shipmentDetails()  
  14.         {  
  15.             return "Shipped";  
  16.         }  
  17.     }  
  18.   
  19.   
  20. using System;  
  21. using System.Collections.Generic;  
  22. using System.Linq;  
  23. using System.Text;  
  24. using System.Threading.Tasks;  
  25. using System.Data;  
  26. using System.Collections;  
  27.   
  28. namespace ConsoleApplication  
  29. {  
  30.     class Program   
  31.     {  
  32.         static void Main(string[] args)  
  33.         {  
  34.               
  35.             //Abstract Class Output  
  36.             getShipping g = new getShipping();  
  37.   
  38.             Console.WriteLine( g.shipmentDetails());  
  39.   
  40.             Console.WriteLine(g.caluculate());  
  41.   
  42.             Console.ReadLine();  
  43.   
  44.         }  
  45.     }  
  46. }  

 

Ebook Download
View all
Learn
View all