0% found this document useful (0 votes)
7 views19 pages

OOPS - AND - Class - Practice

The document provides examples of various object-oriented programming concepts in C#, including abstract classes, static classes, partial classes, sealed classes, singleton classes, encapsulation, inheritance, polymorphism, abstraction, exception handling, and generics. Each concept is illustrated with code snippets demonstrating their usage. Additionally, it touches on data structures like lists, array lists, and hash tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

OOPS - AND - Class - Practice

The document provides examples of various object-oriented programming concepts in C#, including abstract classes, static classes, partial classes, sealed classes, singleton classes, encapsulation, inheritance, polymorphism, abstraction, exception handling, and generics. Each concept is illustrated with code snippets demonstrating their usage. Additionally, it touches on data structures like lists, array lists, and hash tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Abstract class:

using System ;
public abstract class Robot{

public abstract string Shallow(string msg);

public int Hollow(int n){


return n;
}
}

class Meter: Robot{

public override string Shallow(string msg) {


return msg;
}
}

class program{

public static void Main(string[] args){

Meter mt = new Meter();

Console.WriteLine(mt.Shallow("Hello"));

}
}
Static class:

using System;

public static class Helper{

public static int Add(int a , int b){

return a+b;
}
}

class Program{

public static void Main(string[] args){

Console.WriteLine(Helper.Add(3,5));
}
}
Partial Class:
using System;

public partial class ExtensionClass{

public void Bank(){

Console.WriteLine("Hello");
}
}

class Program{

public static void Main(string[] args){

ExtensionClass ec = new ExtensionClass();

ec.Bank();
}
}
Sealed Class:

using System;

public sealed class Debug{

public void Mug(){

Console.WriteLine("can you?");
}

public class Happy:Debug{

class Program{

public static void Main(string[] args){

Happy db = new Happy();

db.Mug(); //not possible


}}
Singleton class:

using System;

public class Hero{

private Hero(){

private static Hero _instance;

public static Hero instance{

get{

if(_instance ==null){

_instance = new Hero();


}
return _instance;

}
//Why must the instance be static in a Singleton?
1. Singleton ensures only one instance of the class exists.
o If the instance property were non-static, you would need to create an object
first to access it.
o But the constructor is private, so you cannot create an object → making it
impossible to access the instancw
public void Meow(){

Console.WriteLine("Hello");
}
}

class Program{

public static void Main(string[] args){

Hero ob = Hero.instance;
ob.Meow();

}
}
OOPS

Encapsulation:

using System;

class BankAccount{

public BankAccount(){

private double _Balance;

public void Credit(double balance){


_Balance += balance;

public void Debit(double balance){

if(balance >0 && balance <=_Balance){


_Balance -= balance;
}
}

public double BankBalance{


get{return _Balance;}
}
}

class Program{

public static void Main(string[] args){

BankAccount ba = new BankAccount();

ba.Credit(1000);
ba.Debit(50);

Console.WriteLine(ba.BankBalance);

}
}
Inheeritance:

using System;

class Animal{

public virtual void AnimalSound(){

Console.WriteLine("Animal sound");
}
}

class Cat:Animal{

public override void AnimalSound(){

Console.WriteLine("Cat says meow");


}
}
class Program{
public static void Main(string[] args){

Cat an = new Cat();

an.AnimalSound();

}
}
Polymorphism:

using System;

class Techno{

public int Add(int a){

return a;
}

public int Add(int a, int b ){

return a+b;
}

public virtual int Add(int a, int b , int c){

return a+b+c;
}

class Gamerz:Techno{

public override int Add(int a , int b, int c){


int ans = a*b*c;

return ans;
}
}

class Program{

public static void Main(string[] args){

Techno tc = new Techno();

Console.WriteLine(tc.Add(5));
Console.WriteLine(tc.Add(5,5));
Console.WriteLine(tc.Add(5,5,5));

Gamerz gm = new Gamerz();

Console.WriteLine(gm.Add(5,5,5));

}
}
Absraction:

using System;

public abstract class Harvard{

public Harvard(){
Console.WriteLine("Hello");
}

public abstract int Define(int n );

public void Jug(){


Console.WriteLine("Hi Jug");
}
}

public interface iKangaroo{

void VivRichards();

int Stats();
}
class lemon:Harvard,iKangaroo{

public lemon():base(){

public override int Define(int n ){

return n;
}

public void VivRichards(){


Console.WriteLine("greatBatsman");
}

public int Stats(){

return 50;
}
}

class Program{

public static void Main(string[] args){

lemon lo = new lemon();


lo.Jug();
Console.WriteLine(lo.Define(5));
lo.VivRichards();
Console.WriteLine(lo.Stats());

}
}
Exceptional Handling:

using System;

class Program{

public void DivideByZero(){

try{
int a =1; int b =0;
int ans = a/b;
Console.WriteLine(ans);

}
catch(Exception ex){
Console.WriteLine(ex.StackTrace);
Console.WriteLine(ex.Message);
}
finally{

}
}

public static void Main(string[] args){


Program pp = new Program();

try{
pp.DivideByZero() ;
}
catch(Exception){
throw;
}
finally{

Console.WriteLine("Inside finally");
}

}
}
Generics:

using System;

public class Vivo<T>{

public void phone(T value){

Console.WriteLine(value);
}
}

class Program{

public static void Main(string[] args){

Vivo<int> vv = new Vivo<int>();

vv.phone(5);

Vivo<string> v = new Vivo<string>();

v.phone("Bismillah");

}
}
List<int> ll = new List<int>();

ArrayList al = new ArrayList();

Hashtable ht = new hashtable();


Delegate:

You might also like