interfaces.md - brainchildservices/curriculum GitHub Wiki
Slide 1
Interface
Interface is another option to achieve abstraction along with abstract classes.
An interface is a completely "abstract class", which can only contain abstract methods and properties (with empty bodies):
interface is defined using the interface keyword instead of the class keyword we use when we create a class.
-
It is considered good practice to start with the letter "I" at the beginning of an interface, as it makes it easier for yourself and others to remember that it is an interface and not a class.
-
You cannot apply access modifiers to interface members. All the members are public by default.
Error: _If you use an access modifier in an interface, then the C# compiler will give a compile-time error "The modifier 'public/private/protected' is not valid for this item.". _
https://dotnetfiddle.net/LvAGq1 (Error: The modifier is not valid for this item) The right way to do it:
https://dotnetfiddle.net/sVeX8J
Slide 2
An example of Geometry Interface that has Area method. Different Shapes can inherit from Geometry Interface and implement their own Area calculations inside the Area method.
https://dotnetfiddle.net/UAKYWa
-
A class that implements from an Interface must implement all the methods of the Interface: Error: https://dotnetfiddle.net/m09Kst - The class doesn't implement one of the methods of the interface. The right way to do it:
https://dotnetfiddle.net/CI4rdZ -
Q1 What is the need of interface when we have the abstract class to define abstract methods?
- To achieve security - hide certain details and only show the important details of an object (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. Note: To implement multiple interfaces, separate them with a comma https://dotnetfiddle.net/BpPnls
Slide 2 Downwards
-
Q2: How interface is different from a class?
An interface is different from a class in the following ways:
We cannot instantiate an interface.
An interface does not contain any constructor or data fields or destructor, etc.
All of the methods of an interface are abstract and public by default.
An interface is not extended by a class; it is implemented by a class.
An interface can extend multiple interfaces. -
Q3: What are the similarities between the interface and abstract class?
An interface is similar to an abstract class in the following ways
Both interface and the abstract class cannot be instantiated means we cannot create the object.
But we can create a reference variable for both interface and abstract class.
The subclass should implement all abstract methods.
Both cannot be declared as sealed.
Slide 2 Downwards
- Q4: What are the differences between the interface and abstract class?
Slide 2 Downwards
Exercises:
- Q1: Define the interface IBattle, which contains the functions Attack(), Move(), Jump();
Define the interface IRest, which contains Function SitDown(), Sleep().
Define the interface IPlayer, and the interface IPlayer inherits IBattle and IRest.
Define Soldier class implements IPlayer interface.
Attach method should print "Attach enemy!"
Move method should print "Move array forward!"
Jump method should print "Jump over the fence!"
SitDown method should print "Sit down and take cover!"
Rest method should print "Troup sleep!"
Call the above methods in the Main method to display the results.
Slide 3
- Q2: Define the interface IUSB, which defines the data reading method ReadData();
Define the abstract class MemoryDevice, this abstract class MemoryDevice implements IUSB interface. The ReadData method prints "Reading data from....". And there is an abstract method Description.
There are three types of harddisk:SSD, HardDisk and UDisk. Create classes for each of the harddisk type and inherit the abstract class MemoryDevice.
SSD: ReadData method should print - "Reading from SSD", Description method should print "I am a solid state drive, and my read and write speeds are fast!"
HardDisk : ReadData method should print - "Reading from HDD", Description method should print "I am a mechanical hard drive, and my read and write speeds are average!"
UDisk : ReadData method should print - "Reading from USB Flash drive", Description method should print "I am a usb flash drive, and my read and write speeds are slow!"
Slide 3 Downwards
- Q3: Create a C# program that implements an IVehicle interface with two methods, one for Drive of return type void and another for Refuel of return type bool that has a parameter of type integer with the amount of gasoline to refuel. Then create a Car class with a constructor that receives a parameter with the car's starting gasoline amount and implements the Drive and Refuel methods of the car.
The Drive method will print on the screen that the car is Driving, if the gasoline is greater than 0. The Refuel method will increase the gasoline of the car and return true.
To carry out the tests, create an object of type Car with 0 of gasoline in the Main of the program and ask the user for an amount of gasoline to refuel, finally execute the Drive method of the car.
MCQ:
https://www.interviewsansar.com/programming-exercises-solutions-c-interface/
References:
https://dotnettutorials.net/lesson/interface-c-sharp/
https://www.w3schools.com/cs/cs_interface.asp
https://www.tutorialsteacher.com/csharp/csharp-interface
https://www.c-sharpcorner.com/blogs/multiple-inheritance-in-c-sharp-using-interfaces1
https://www.programmersought.com/article/44684977808/ https://www.exercisescsharp.com/oop/interfaces/
Main Exercise:
http://www.cs.columbia.edu/~lok/csharp/assignments.html