Interface and abstraction

Post on 11-Nov-2014

115 views 2 download

Tags:

description

 

Transcript of Interface and abstraction

About Abstract Class

• Abstract class in csharp is defined using "abstract" keyword• An abstract class may contain abstract methods and

accesses (properties).• Non abstract methods or properties should provide actual

implementation in an abstract class.

• Abstract classes that cannot be instantiated mean we cannot able to create an object of an abstract class but abstract class can be implemented to child classes like a common base class. An abstract class can be partially implemented or not at all implemented.

Syntax

1 abstract class absCalculator2 ...code starts here3 }

Abstract Method

In an abstract class a method which has a keyword "abstract" and doesn't provide any implementation is called abstract method.The implementation logic of abstract methods is provided by the child classes or derived classes.Child classes use keyword "override" with same method name (as abstract method name) to provide further implementation of abstract methods.

Non Abstract Method

In an abstract class a method which doesn't have a keyword "abstract" and provide any implementation is called non abstract method.

Why Abstract Class

Abstract class enforces derived classes to provide all implementation logic for abstract methods or properties.

create an abstract class "absCalculate" having abstract methods and non abstract methods.

01 abstract class absCalculate02 03 //A Non abstract method04 public int Addition(int Num1, int Num2)05 06 return Num1 + Num2;07 }08 09 //An abstract method, to be overridden in derived class10 public abstract int Multiplication(int Num1, int Num2);11 }

create a class "clsCalculate" and implement it with abstract class "absCalculate".

1 class clsCalculate:absCalculate2 3 ...code4 }

Now lets give implementation for an abstract method using override keyword implementing

the abstract method

1 class clsCalculate:absCalculate2 3 4 //using override keyword implementing the abstract method5 public override int Multiplication(int Num1, int Num2)6 7 return Num1 * Num2;8 }9 }

create an object of class "clsCalculate" in our main program of an console application

01 class Program02 03 04 static void Main(string[] args)05 06 07 Console.WriteLine("Please Enter First Number");08 int num1 = Convert.ToInt16(Console.ReadLine());09 10 Console.WriteLine("Please Enter Second Number");11 int num2 = Convert.ToInt16(Console.ReadLine());12 13 absCalculate objabCal = new clsCalculate();14 int add = objabCal.Addition(num1, num2);15 int multiplied = objabCal.Multiplication(num1, num2);16 Console.WriteLine("Added Number is : 0}, Multiplied Number is : 1}", add, multiplied);17 }18 }

Output

What is an Interface?• An interface looks like a class but has got no implementation. In an

interface we cannot do any implementation but we can declare signatures of properties, methods, delegates and events.

• Implementation part is been handle by the class that implements the interface. Implemented interface enforces the class like a standard contract to provide all implementation of interface members.

• We can implement single interface or multiple interfaces to the class. By default interfaces are public.

• We declare interface by using "interface" keyword.

interface "IEmployee" with signature of a method "DisplayEmployeeDetails".

• 1 interface IEmployee{• 2 • 3 void DisplayEmployeeDetails();• 4 • 5 }

Various Forms of implementing interface

There are two of ways of implementing interfaces

• Explicit• Implicit

Explicit interface implementation

• When we have two different interfaces with same method name then in that scenario we can use explicit interface implementation.

• To implement an interface explicitly we have to define an interface name followed by (".") dot operator then the method name

we have two different interfaces "IEmployee" and "ICompany" with same method name.

1 public interface IEmployee{2 void DisplayEmployeeDetails();3 }4 5 public interface ICompany{6 void DisplayEmployeeDetails();7 }

In order to implement an interface explicitly we have to define the interface name followed by

(".") dot operator before method name1 public class Employee : IEmployee,ICompany2 {3 void ICompany.DisplayEmployeeDetails(){4 Console.WriteLine("ICompany Employee Name --> Questpond and Employee Code --> 009");5 }6 void IEmployee.DisplayEmployeeDetails(){7 Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");8 }9 }

create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a

Console Application program.

01 class Program{02 03 static void Main(string[] args)04 {05 IEmployee IEmp = new clsEmployee();06 IEmp.DisplayEmployeeDetails();07 08 ICompany IComp = new clsEmployee();09 IComp.DisplayEmployeeDetails();10 }11 12 }

Output

Implicit interface implementation

demonstration of an interface "IEmployee" with signature of a method "DisplayEmployeeDetails".

1 interface IEmployee2 {3 void DisplayEmployeeDetails();4 }

create a class "Employee" and implement the interface implicitly

1 public class Employee : IEmployee2 {3 public void DisplayEmployeeDetails()4 {5 Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");6 }7 }

create the objects of an interface "IEmployee" in our main method of a

Console Application program

1 public class Program{2 3 static void Main(string[] args)4 {5 IEmployee IEmp = new clsEmployee();6 IEmp.DisplayEmployeeDetails();7 }8 9 }

Output