Thursday, July 23, 2015

Abstract Classes in C#

Hi guys
In this post we will learn about abstract classes in C#.

What do you mean by word “Abstract”?
Abstract means (as a noun) summary not a whole story.

"Take vehicle and car, vehicle is an abstract of car."

So in an abstract class we have abstract and non-abstract methods. In the class abstract methods don’t have implementation; we can implement those later in derived classes.
We can create an abstract class through using the abstract keyword. We cannot have an instance of abstract class.
If we try to make an instance of abstract class we get a compile time error "cannnot create an instance of abstract class or interface".
So why we need abstract classes? We can have abstract classes just for inheritance. It provides the a common pattern to all derived classes.

Some basic facts about abstract class
  • It is mandatory to override all abstract method in the derived class.
  • Abstract classes are only for inheritance. 
  • An abstract class can also contain methods with complete implementation, besides abstract methods.
  • Abstract classes have effect only when used with inheritance.
  • An abstract member is not implemented in the base class and must be implemented in derived classes.
  • A member defined as virtual must be implemented in the base class, but may be optionally overridden in the derived class if different behavior is required.
  • An abstract class cannot support multiple inheritances.
  • Abstract methods cannot have body.
Here is an example code of abstract class and it's output.

 using System;  
 namespace ConsoleApplication1  
 {  
   abstract class AbsClass  //declaring abstract class  
   {  
     //declaring abstract methods  
     public abstract int MultiplyTwoNumbers(int x, int y);  
     public abstract int SubtractTwoNumbers(int p, int q);  
     //declaring non-abstract method  
     public int AddThreeNumbers(int a, int b, int c)  
     {  
       return a + b + c;  
     }  
   }  
   class DerivedClass : AbsClass // derived class inheriting the abstract class  
   {  
     // overring the abstract methods in derived class   
     public override int MultiplyTwoNumbers(int x, int y)  
     {  
       return x * y;  
     }  
     public override int SubtractTwoNumbers(int p, int q)  
     {  
       return p - q;  
     }  
   }  
   class Program  
   {  
     static void Main()  
     {  
       DerivedClass obj = new DerivedClass(); //created an object of derived class  
       // calling all methods with signatures   
       int Addition = obj.AddThreeNumbers(10, 20, 50);   
       int Multipication = obj.MultiplyTwoNumbers(3, 50);  
       int Substraction = obj.SubtractTwoNumbers(10, 3);  
       // not printing the output  
       Console.Write("Addition of 10, 20 and 50 = " + Addition.ToString() + "\n" + "Mutiplication of 3 and 50 = " + Multipication.ToString() + "\n" + "Subtraction of 10 and 3 = " + Substraction.ToString());  
       Console.ReadLine();  
     }  
   }  
 }  

Output :









I hope it would be helpful.
Thanks

Friday, July 10, 2015

Use of self join in SQL

Suppose we have an Employee table. All the employees  and their supervisor (or Manager) are in the same table. If we want to fetch the employees and their supervisors, how we could get them?

For that you need to choose self join. Self join is an inner join but only applied with one table. For applying self join we need 2 instance of the same table in memory. We have to use alias for making identical copies of the table because there is only one table.

See the example below.
Lets create a employee table first.
CREATE table mtblEmployee
(Employee_Id int identity (1,1),
Employee_Name nvarchar(50),
Supervisor_Id int
)
Now insert some value in this table
INSERT INTO mtblEmployee
SELECT 'Amit Mittal',0
UNION ALL 
SELECT 'Piyush Sharma',3
UNION ALL 
SELECT 'Dinesh Aggarwal',6
UNION ALL
SELECT 'Shobhit Roy',5
UNION ALL
SELECT 'Varun Dhiman',1

fetch the records
select * from mtblEmployee









By using self join we can fetch the employee and their supervisors

The below both query will give the same result
SELECT a.Employee_Id as Id, a.Employee_Name as Employee,
b.Employee_Name as Supervisor
FROM mtblEmployee a 
INNER JOIN  mtblEmployee b
on a.Supervisor_Id=b.Employee_Id
OR
SELECT a.Employee_Id as Id, a.Employee_Name as Employee,
b.Employee_Name as Supervisor 
FROM mtblEmployee a, mtblEmployee b 
where a.Supervisor_Id=b.Employee_Id
Result:








I hope it would help.

Thanks

LinkWithin

Related Posts Plugin for WordPress, Blogger...