Interface
- Completely "abstract class", which can only contain abstract methods and properties (with empty bodies).
- By default, members of are abstract and public.
- Can contain properties and methods, but not fields.
- The body must be provided by the "derived" class without using override keyword.
- Virtual method is not allowed in interface.
- Cannot contain a constructor.
- Multiple inheritance can achieved.
- It can't be instantiate.
Abstract Class
- It is Restricted class, that cannot be instantiate.
- To access it, it must be inherited from another class.
- Typically used to define a base class in the class hierarchy.
- An abstract class can have both abstract and regular methods.
Wrong
- This class must contain at least one abstract method.
- Abstract classes are either partially implemented or not implemented at all.
eg.
public interface IInterface // sealed, private, protected, protected internal are not possible, public is optional (by default public){ void demo();
void demo1();
}public class InterfaceDemoTest : IInterface
{
public void demo()
{
Console.WriteLine("Demo Method Called");
}
public void demo1()
{
Console.WriteLine("Demo1 Method Called");
}
}
}
Difference between Abstract class and
Interface |
|
Abstract |
Interface |
Can have abstract and regular(concrete) methods both |
Can have only abstract methods without abstract keyword |
Can have Constructor |
Can’t have Constructor |
Can create object as reference to child class AbstractClass abs = new AbstractDemoTest(); |
Can create object as reference to child class IInterface iinterface = new InterfaceDemoTest(); |
By default private class |
By default public interface |
Access modifiers required for methods |
Access modifiers not required for methods |
Multiple inheritances can’t achieve. |
Multiple inheritances can achieve. |
Can contain fields |
Can’t contain fields |
Only Abstract methods must be implement in derived class |
All methods must be implement in derived class |
Used for inheritance |
Used for inheritance |
Similarities between Abstract class and
Interface |
Can’t Create Instance |
Abstract methods must me implement in derived class |
Used for inheritance |
Used as Parent in inheritance |
No comments:
Post a Comment
This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.