2. 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.
4. 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.
5. 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.
6. Why Abstract Class
Abstract class enforces derived classes to
provide all implementation logic for abstract
methods or properties.
7. 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);
}
8. create a class "clsCalculate" and
implement it with abstract class
"absCalculate".
1
2
3
4
class clsCalculate:absCalculate
...code
}
9. 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
}
10. 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
}
12. 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.
13. interface "IEmployee" with signature
of a method
"DisplayEmployeeDetails".
•
•
•
•
•
1 interface IEmployee{
2
3 void DisplayEmployeeDetails();
4
5 }
14. Various Forms of implementing
interface
There are two of ways of implementing
interfaces
• Explicit
• Implicit
15. 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
16. 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();
}
17. 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
}
18. 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();
}
}
21. demonstration of an interface
"IEmployee" with signature of a
method "DisplayEmployeeDetails".
1
2
3
4
interface IEmployee
{
void DisplayEmployeeDetails();
}
22. 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
}
23. 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();
}
}