Class: A class is a collection of properties and methods.
Example code
- public class cart
- {
- int cartID;
- List<string> list = new List<string>();
- ArrayList aList = new ArrayList();
-
- public List<string> getCartDetails(int pcartID)
- {
-
- cartID = pcartID;
-
- list.Add("Product Name");
- list.Add("Description");
- list.Add("Size");
- return list;
-
- }
- }
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Collections;
-
- namespace ConsoleApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- cart cObj = new cart();
-
-
-
- List<string> range = cObj.getCartDetails(1);
- foreach (string item in range)
- {
- Console.WriteLine(item);
- }
-
- Console.ReadLine();
- }
- }
- }
Object: An object is an instance of a class.
- cart cObj = new cart();
- cObj.getCartDetails(1);
Inheritance
When a class of data wants to derive another class.
Advantage of inheritance: Reusability of class data.
Example code
- public class users
- {
- public string userName { get; set; }
- public string passWord { get; set; }
- }
- public class Register : users
-
- {
- public int mobileNumber { get; set; }
- public string address { get; set; }
-
-
- }
-
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Collections;
-
- namespace ConsoleApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
-
-
- Register objUsers = new Register();
- objUsers.userName = "RajeshGonugunta";
- objUsers.passWord = "ppppp";
- objUsers.mobileNumber = 99999;
- objUsers.address = "Address1";
-
-
-
-
- List<Register> objUsersList = new List<Register>();
- objUsersList.Add(objUsers);
-
- foreach (Register list in objUsersList)
- {
- Console.WriteLine(list.userName);
- Console.WriteLine(list.passWord);
-
- Console.WriteLine(list.mobileNumber);
- Console.WriteLine(list.address);
- }
- }
- }
- }
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
- public abstract class shipping
- {
- public string delivery { get; set; }
- public abstract string shipmentDetails();
- public bool caluculate()
- {
- return true;
- }
-
- }
- public class getShipping :shipping
- {
- public override string shipmentDetails()
- {
- return "Shipped";
- }
- }
-
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Collections;
-
- namespace ConsoleApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- getShipping g = new getShipping();
-
- Console.WriteLine( g.shipmentDetails());
-
- Console.WriteLine(g.caluculate());
-
- Console.ReadLine();
-
- }
- }
- }