-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- Interface And Multiple Inheritance Through Interfaces
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Abstraction through Interface in C#
- Multiple Inheritance through Interfaces
Interface in C#
- Apart from Abstract Classes, another way to achieve abstraction in C#, is with interfaces.
- An interface is a completely "abstract class", which can only contain abstract methods and properties (with empty bodies)
- Interface is a C# type definition block which is 100% abstract.
- All the Interface methods by default public and abstract.
- Like abstract classes, interfaces cannot be used to create objects.
- An interface cannot contain a constructor (as it cannot be used to create objects).
- To access the interface methods, the interface must be "implemented" by another class. To implement an interface, use the : symbol (just like with inheritance). The body of the interface method is provided by the "implement" class.
- A class that implements an interface must implement all the methods declared in the interface.
- C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. To implement multiple interfaces, separate them with a comma.
Example:
interface Vehicle
{ void engine();
void wheels();
void frontlight();
void backlight();
void horn();
void seat();
}
Abstraction Example in C#
interface LuxaryPersonalCar
{ void ac();void music(); }
public class Car : Vehicle, LuxaryPersonalCar
{ public void backCamera(){ Console.WriteLine("Good Quality back camera"); //method specific to Class }
public void ac(){ Console.WriteLine("Good Quality AC Serice"); }
public void backlight(){ Console.WriteLine("2 Backlight"); }
public void engine(){ Console.WriteLine("1200 CC Engine"); }
public void frontlight(){ Console.WriteLine("2 Front Light"); }
public void horn(){ Console.WriteLine("Good Quality 1 horn"); }
public void music()
{ Console.WriteLine("Good Quality Music Player"); }
public void seat(){ Console.WriteLine("Total 5 Adult Seats"); }
public void wheels()
{ Console.WriteLine("Total 5 Wheels"); } }
static void Main(){ Car hyndaii20 = new Car();
hyndaii20.horn();
hyndaii20.engine();
hyndaii20.ac();
hyndaii20.seat();hyndaii20.backCamera(); }