0
About Abstract Class
• Abstract class in csharp is defined using "abstract"
keyword
• An abstract class may contain abstra...
Syntax
1
2
3

abstract class absCalculator
...code starts here
}
Abstract Method
In an abstract class a method which has a
keyword "abstract" and doesn't provide any
implementation is cal...
Non Abstract Method
In an abstract class a method which doesn't
have a keyword "abstract" and provide any
implementation i...
Why Abstract Class
Abstract class enforces derived classes to
provide all implementation logic for abstract
methods or pro...
create an abstract class "absCalculate" having
abstract methods and non abstract methods.
01
02
03
04
05
06
07
08
09
10
11...
create a class "clsCalculate" and
implement it with abstract class
"absCalculate".
1
2
3
4

class clsCalculate:absCalculat...
Now lets give implementation for an abstract
method using override keyword implementing
the abstract method
1
class clsCal...
create an object of class "clsCalculate" in our
main program of an console application
01
class Program
02
03
04
static vo...
Output
What is an Interface?
• An interface looks like a class but has got no implementation. In an
interface we cannot do any im...
interface "IEmployee" with signature
of a method
"DisplayEmployeeDetails".
•
•
•
•
•

1 interface IEmployee{
2
3 void Disp...
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
c...
we have two different interfaces "IEmployee"
and "ICompany" with same method name.
1
2
3
4
5
6
7

public interface IEmploy...
In order to implement an interface explicitly we
have to define the interface name followed by
(".") dot operator before m...
create the objects of two interfaces "IEmployee"
and "ICompany" in our main method of a
Console Application program.
01
02...
Output
Implicit interface implementation
demonstration of an interface
"IEmployee" with signature of a
method "DisplayEmployeeDetails".
1
2
3
4

interface IEmploye...
create a class "Employee" and
implement the interface implicitly
1
public class Employee : IEmployee
2
{
3
public void Dis...
create the objects of an interface
"IEmployee" in our main method of a
Console Application program
1
2
3
4
5
6
7
8
9

publ...
Output
Interface and abstraction
Upcoming SlideShare
Loading in...5
×

Interface and abstraction

55

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
55
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Interface and abstraction"

  1. 1. 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.
  2. 2. Syntax 1 2 3 abstract class absCalculator ...code starts here }
  3. 3. 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.
  4. 4. 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.
  5. 5. Why Abstract Class Abstract class enforces derived classes to provide all implementation logic for abstract methods or properties.
  6. 6. create an abstract class "absCalculate" having abstract methods and non abstract methods. 01 02 03 04 05 06 07 08 09 10 11 abstract class absCalculate //A Non abstract method public int Addition(int Num1, int Num2) return Num1 + Num2; } //An abstract method, to be overridden in derived class public abstract int Multiplication(int Num1, int Num2); }
  7. 7. create a class "clsCalculate" and implement it with abstract class "absCalculate". 1 2 3 4 class clsCalculate:absCalculate ...code }
  8. 8. Now lets give implementation for an abstract method using override keyword implementing the abstract method 1 class clsCalculate:absCalculate 2 3 4 //using override keyword implementing the abstract method 5 public override int Multiplication(int Num1, int Num2) 6 7 return Num1 * Num2; 8 } 9 }
  9. 9. create an object of class "clsCalculate" in our main program of an console application 01 class Program 02 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 }
  10. 10. Output
  11. 11. 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.
  12. 12. interface "IEmployee" with signature of a method "DisplayEmployeeDetails". • • • • • 1 interface IEmployee{ 2 3 void DisplayEmployeeDetails(); 4 5 }
  13. 13. Various Forms of implementing interface There are two of ways of implementing interfaces • Explicit • Implicit
  14. 14. 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
  15. 15. we have two different interfaces "IEmployee" and "ICompany" with same method name. 1 2 3 4 5 6 7 public interface IEmployee{ void DisplayEmployeeDetails(); } public interface ICompany{ void DisplayEmployeeDetails(); }
  16. 16. In order to implement an interface explicitly we have to define the interface name followed by (".") dot operator before method name 1 public class Employee : IEmployee,ICompany 2 { 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 }
  17. 17. create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a Console Application program. 01 02 03 04 05 06 07 08 09 10 11 12 class Program{ static void Main(string[] args) { IEmployee IEmp = new clsEmployee(); IEmp.DisplayEmployeeDetails(); ICompany IComp = new clsEmployee(); IComp.DisplayEmployeeDetails(); } }
  18. 18. Output
  19. 19. Implicit interface implementation
  20. 20. demonstration of an interface "IEmployee" with signature of a method "DisplayEmployeeDetails". 1 2 3 4 interface IEmployee { void DisplayEmployeeDetails(); }
  21. 21. create a class "Employee" and implement the interface implicitly 1 public class Employee : IEmployee 2 { 3 public void DisplayEmployeeDetails() 4 { 5 Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009"); 6 } 7 }
  22. 22. create the objects of an interface "IEmployee" in our main method of a Console Application program 1 2 3 4 5 6 7 8 9 public class Program{ static void Main(string[] args) { IEmployee IEmp = new clsEmployee(); IEmp.DisplayEmployeeDetails(); } }
  23. 23. Output
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×