Interface and abstraction

24

description

 

Transcript of Interface and abstraction

Page 1: Interface and abstraction
Page 2: 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.

Page 3: Interface and abstraction

Syntax

1 abstract class absCalculator2 ...code starts here3 }

Page 4: Interface and abstraction

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.

Page 5: Interface and abstraction

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.

Page 6: Interface and abstraction

Why Abstract Class

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

Page 7: Interface and abstraction

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 }

Page 8: Interface and abstraction

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

1 class clsCalculate:absCalculate2 3 ...code4 }

Page 9: Interface and abstraction

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 }

Page 10: Interface and abstraction

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 }

Page 11: Interface and abstraction

Output

Page 12: Interface and abstraction

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.

Page 13: Interface and abstraction

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

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

Page 14: Interface and abstraction

Various Forms of implementing interface

There are two of ways of implementing interfaces

• Explicit• Implicit

Page 15: Interface and abstraction

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

Page 16: Interface and abstraction

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 }

Page 17: Interface and abstraction

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 }

Page 18: Interface and abstraction

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 }

Page 19: Interface and abstraction

Output

Page 20: Interface and abstraction

Implicit interface implementation

Page 21: Interface and abstraction

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

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

Page 22: Interface and abstraction

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 }

Page 23: Interface and abstraction

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 }

Page 24: Interface and abstraction

Output