In C#, if you wanted to do multiple inheritance like that in C++, its not allowed. However, there is a workaround or another way to do this, by creating a class interface. An interface can have methods, properties and indexers, but no implementation is allowed. Only in the class object that the interface is being inherited from is implementation allowed. So interfaces: ONLY DECLARATION, NO IMPLEMENTATION.
Here's what I'm talking about. Writing an interface is like writing an abstract base class. However, one rule that stands firm is: Interfaces by default are PUBLIC, while classes are internal and can be private, public, protected, sealed, partial, etc.
interface IRead
{
void Read();
}
interface IWrite
{
void Write();
}
class NotePad: IRead, IWrite //multiple inheritance
{
//Notepad class will implement the two methods, Read and Write inside the class
public void Write()
{
Console.WriteLine("This method writes on the console window, its inherited from IWrite");
}
public void Read()
{
Console.ReadLine();//keeps the console window open so we can read the message
}
}
class Program
{
static void Main()
{
NotePad n = new NotePad();
n.Write();
n.Read();
//you can also do this as well
IWrite w = new NotePad();
w.Write();
IRead r = new NotePad();
r.Read();
}
}
Here's what I'm talking about. Writing an interface is like writing an abstract base class. However, one rule that stands firm is: Interfaces by default are PUBLIC, while classes are internal and can be private, public, protected, sealed, partial, etc.
interface IRead
{
void Read();
}
interface IWrite
{
void Write();
}
class NotePad: IRead, IWrite //multiple inheritance
{
//Notepad class will implement the two methods, Read and Write inside the class
public void Write()
{
Console.WriteLine("This method writes on the console window, its inherited from IWrite");
}
public void Read()
{
Console.ReadLine();//keeps the console window open so we can read the message
}
}
class Program
{
static void Main()
{
NotePad n = new NotePad();
n.Write();
n.Read();
//you can also do this as well
IWrite w = new NotePad();
w.Write();
IRead r = new NotePad();
r.Read();
}
}
The following are some important things about interfaces:
- We cannot create an object of an interface, we can only create a reference or Interface Variable.
- An interface will have only abstract methods (method declaration).
- Interfaces supports Inheritance, Polymorphism (Overloading, Overriding (using the "new" keyword to hide the interface methods above)).
- We cannot create variables in an interface.
- Only Properties, Indexers, Methods and Events are allowed in an interface.
- A class can inherit multiple interfaces, which is also shown in the snapshot above.
- We cannot create any Access modifiers with Interface Members (like private, public, protected, internal, protected internal, virtual, override, static, abstract etc.)
- The new keyword is allowed in an interface.
- All methods of an interface must be defined in every derived class.
- An interface is good for small or medium level projects.
No comments:
Post a Comment