C#, I’ve learned more about interfaces and now understand how to use them to avoid the multiple Inheritance issue via /r/learnprogramming

C#, I’ve learned more about interfaces and now understand how to use them to avoid the multiple Inheritance issue

A few days ago I asked what the purpose of Interfaces are for and for the most part the answer was they were used as a template for large scale projects. But MSDN suggests that they are used to be an alternative to bypass the inheritance restriction. The problem I had with learning interfaces was I couldn't see their purpose. So I've made a small demo that helped me learn, I hope this may be useful to others.

Not many articles online explain how to do this, but I do know that knowledge of this is required for the 70-483 certification. It actually can be done with the "Quick Actions and Refactorings", keep in mind that I am just a junior programmer, but I have leaned to trust the Visual Studio suggestions for most things…

//The scenario is you're in a Community College, you have the Facilities class which provides your badge, parking clearance and documents your name. //You have the Staff, these are the people that work for the Community College //And of course, you have the Students, who sacrifice their souls to pay for the courses to learn. // But you also have a teacher Assistant. This person is technically a Student and a Staff Member. // You can't inherit both Facilties and Staff for Teacher Assistant without making Student a Staff Member. // So instead, you make an interface to workaround this issue. public enum Parking { P1, P2, Outside }; class Program { static void Main(string[] args) { var T = new TeacherAssistant(Parking.P1, 1001, "Yubario",299,true); T.ReportToProfessor(); Console.WriteLine(T.EmployeeID); } } public class Facilities { //Create a Base Constructor by highlighting the properties then pressing "Control + ." public Facilities(Parking level, int badgeNumber, string name) { Level = level; BadgeNumber = badgeNumber; Name = name; } public Parking Level; public int BadgeNumber { get; } public string Name { get; } } public class Staff : Facilities { //Visual Studio creates this for you by doing the same, "Control+." public Staff(Parking level, int badgeNumber, string name, int EmployeeID,bool AnswerKey) : base(level, badgeNumber, name) { //Since the Staff has an Employee ID and an answerkey I've added more to this constructor //Always keep in mind the base constructor is called first. this.EmployeeID = EmployeeID; this.AnswerKey = AnswerKey; } public bool AnswerKey { get; } public int EmployeeID { get; } } // Write the methods, then use Control + . to select "Extract Interface". This will show you a selection to choose all the public methods you'd like to keep in the Interface. //To see this option, make sure to have the Class highligted in Visual Studio. //The TA has no need to report to TA, so we extract the interface for LoanDebt and ReportToProfessor() public class Student : Facilities, IStudent { //Visual Studio creates this for you by doing the same, "Control+." public Student(Parking level, int badgeNumber, string name) : base(level, badgeNumber, name) { } public int LoanDebt { get; set; } public void SubmitHomeWork() { } public void ReportToTA() { Console.WriteLine("I need some help understanding this subject here, can you help me?"); } public void ReportToProfessor() { Console.WriteLine("Hey professor, did you have a few moments to talk about something?"); } // Obviously a real program would have a lot more complex code, but keeping it simple for now. } public class TeacherAssistant : Staff, IStudent { //This is where we get to be lazy! We're going to declare a property of Student Student sue; public TeacherAssistant(Parking level, int badgeNumber, string name, int EmployeeID, bool AnswerKey) : base(level, badgeNumber, name, EmployeeID, AnswerKey) { sue = new Student(level, badgeNumber, name); //We need to pass the values from the constructor to initiliaze a new student } //Then, we are going to press "Control+." and select "Implement Interface through sue" public int LoanDebt { get => ((IStudent)sue).LoanDebt; set => ((IStudent)sue).LoanDebt = value; } public void ReportToProfessor() { ((IStudent)sue).ReportToProfessor(); } //So, Visual Studio has plugged in code that points to the Student Method automatically. //This allows you to share classes without writing duplicate code. } 

Submitted July 14, 2017 at 10:34PM by yubario
via reddit http://ift.tt/2tTo4eh


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s