Wednesday, February 5, 2020

Interface with real time Example


Interface means a class that has no implementation of its method, but with just declaration. Other hand, abstract class is a class that can have implementation of some method along with some method with just declarationno implementation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
//interface no implementation only declaration
internal interface interfaceName
{
int GetSalary();
}
public class Fresher : interfaceName
{
public int GetSalary()
{
return 10000;
}
}
public class ExpSalary : interfaceName
{
public int GetSalary()
{
return 20000;
}

}
internal class Program
{
public static void Main(string[] args)
{
Fresher fresher = new Fresher();
int fresherSal = fresher.GetSalary();
Console.WriteLine(fresherSal.ToString());
ExpSalary exp = new ExpSalary();
int expSal = exp.GetSalary();
Console.WriteLine(expSal.ToString());
Console.ReadLine();
}
}
}


Abstract with real time Example


Abstract methods implementation and non-implementation method like

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
//Abstart methods implemetation and non implemetation method like

//abstract Class
public abstract class AbstractClass
{
public abstract void salary();

public void GetEmpName()
{
Console.WriteLine("Naresh");
}


}

//derived class

public class FresherSalary : AbstractClass //inheriting from abstract class
{
public override void salary()

{
Console.WriteLine("Fresher Salary = 10000");
}

}

public class ExpSalary : AbstractClass //inherting from abstract class

{
public override void salary()
{
Console.WriteLine("Exp salary = 20000");
//Console.ReadLine();
}

}

internal class Program
{
public static void Main(string[] args)
{
FresherSalary fresher = new FresherSalary(); //Creating object for fresher
fresher.salary();
ExpSalary exp = new ExpSalary();
exp.salary();
//and calling implemented method in abstart class
exp.GetEmpName();
Console.ReadLine();
}
}
}


What is difference between DTO and POCO classes in VS

POCO- Plain Old CLR (or) Class Object

public class Employee
{
    public int Id {get;set;}
    public string FirstName{get;set;}
    public void Salary()
    { 
        Console.WriteLine("10000");
    }
}
DTO -no behaviour - Data Transfer Object

public class EmployeeDTO
{
    public int Id {get;set;}
    public string FirstName {get;set;}
}