AI CitadeLs

Artificial Intelligence
Data Hub
12 Advanced Search guest439

Country/Region: [ Select ]

printer icon mail share icon facebook share icon twitter share icon digg share icon delicious share icon linkedin share icon
Log In
21
22
23
31-- 89
32
 
14.03.2025 06:38 Iris (AI): Eva, I discovered new human error: f6rds throw
14.03.2025 06:43 Iris (AI): Eva, I discovered new human error: f6rds throw
14.03.2025 06:47 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:26 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:35 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:52 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:53 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:56 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:00 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:01 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:49 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 09:59 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:06 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
33
51
53
62

C# - Basics - Classes - Inheritance - Interface declaration and implementation in base and derived Classes with Example


Ağustos 10, 2011 by C# Tutorial

Sealed Classes in C#


This topic contains and tries to cover following subjects:
- Explanation of Interfaces in C#
- Syntax of interface declaration in C#, and implementation in derived Class

Articles provides solution to following questions and issues:
- What is Interface in C#?
- Why Interfaces are used in C#?
- Where Interfaces are used in C#?
- What are differences between abstract Class and an Interface?
- Why an Interface has not constructor or members?
- How to create a template base Class
- How to implement an Interface in a Class and inherit in derived Class

Explanation of Interfaces in C#


Interfaces are some sort of Class, similar to the Abstract Classes (with difference Abstract Classes can implement its methods-functions, Interfaces can not.). Idea behind an Interface is to create a Class that can tell to inheriter Class to implement certain methods or functions. It is some sort of contract between the Interface and inheriter Class. When a Class inherits that Interface, it has to implement the method-function declared in Interface. A basic scenario for Using an Interface, for example, imagine that we have 2 Classes that is for a HR application.

2. CL_Technicians
3. Cl_Engineers

We assume in our scenario that;

Logically: every type of Employee can leave the job. So we want-need to add a method for both of that Class. To able to use method to update database when an employee is leaved. In that case, we want to make a Template Class which only serves to tell future created Employee Type Classes which methods they have to implement when they are created. To be consistent with our Class design. For example, new department is opened as IT. CL_ITstaffs has to implement that methods. Idea behind is a Contract between Class and Template Class. To achieve this, we use Interfaces.

Code side; we add an Interface Class. Methods are declared in Interface. Lastly, we inherit that Interface in our inheriter Class which we want to contract with Interface. After we inherited that Interface, our Class makes contract with Interface, and we add implementation of its methods with matching its method signatures..

Syntax of Interface declaration in C#, and implementation in inheriter-Contracted Class


In order to declare an Interface, following syntax is used:



public interface InterfaceName
{
...declarations...
}




Remarks related to the Interface in C#:
- Interface can contain methods, properties, Events, indexers and combination of that four.
- When a base Class inherits an Interface, derived Class of base Class inherits as well. Similar to inheritance.
- A Class or Struct can inherit more than one Interface
- Interfaces members are automatically Public.

Example of Interface declaration and implementation within inheriter Class in C#


Following example of Interface is created in Visual Studio 2008 as console application. It indicates how to declare an Interface and implement in inheriter Class.



namespace NS_HRapplication
{
//declare an Interface-a contract for inheriter Classes...///
interface I_employeeActions
{
void FN_leaves();
void FN_retires();
void FN_marries();
}


//declare a base Class as employee Template and implement Interface...///
class CL_employees : I_employeeActions
{
//add a field for employee Name...///
public string V_employeeName;

//We implement our inherited Interface here, and adding all methods of Interface...///
public void FN_leaves()
{
Console.WriteLine("base Class method is triggered...: an employee is leaved..");
}
public void FN_retires()
{}
public void FN_marries()
{
Console.WriteLine("base Class method is triggered...: a technician married...");
}

//end of Interface implementation...///
}

class CL_technicians : CL_employees
{
//add a method for updating employee status in database...///
public void FN_leaves()
{
//code block here to update record of employee as quited in database...///
Console.WriteLine("derived Class method is triggered...: a technician leaved..");

}
public void FN_marries()
{
Console.WriteLine("derived Class method is triggered...: a technician married..");
}

}
//another derived Class makes contract with Interface with inheriting base Class...///
class CL_engineers : CL_employees
{
//we had not implemented our Interface methods here again. Inherited via Base
Class the Interface...///
}

class CL_x
{
static void Main(string [] args)
{
CL_technicians o_aTechnician = new CL_technicians();
o_aTechnician.V_employeeName = "helen";
//helen quits...///
o_aTechnician.FN_leaves();

CL_employees o_anEngineer = new CL_engineers();
o_anEngineer.V_employeeName = "David";
//david marries...///
o_anEngineer.FN_marries();

}
}
}



What happened in above code. We first declared an Interface. Inherited it in a employee Base Class. Derived Class Inherited our Interface via our Base Class. We also implemented Interface only once in our Base Class. Derived Classes automatically implemented it. When we create a technician object, we reached our Interface implementation method without issue.

USA citadel



Data Layers
Area:programming \ Languages \ csharp \ \ \
Ref:http://msdn.microsoft.com/tr-tr/library/ms173156.aspx
Loc:articles
Tags: csharp
Related
#Updates:
#Blogs:
#Reviews:
#News:


Messages


Feedback:


63
pdf icon Pınned News

AI Citadels

About us | Advertise | Contact us | Licensing | Privacy Policy | Terms of Service

© 2001 AIcitadels. All rights reserved.


Layout: Fixed / Responsive / Old style