Abstract Classes & Interface
Abstract Classes & Interface
Abstract Classes & Interface
An abstract class is a class that cannot be instantiated and must be inherited. An Abstract class
contains abstract and non-abstract method(s). An abstract method needs to be overridden and
implemented in a derived non-abstract class. An abstract class is essentially a blueprint for a class
without any implementation.
Example:
--------------------
Code C# Abstract Class
--------------------
public abstract class Car Declare Abstract Method
{
protected int headlights = 100; // Declare Variable value
public abstract void price(); // Abstract Method
public void clsbaseMethod() // NON-Abstract Method
{
Console.WriteLine("it a Non-abstract method");
}
}
Define Abstract Method
public class tool : Car
{
public override void price()
{
Console.WriteLine("Child Move");
}
public new void clsbaseMethod()
{
Console.WriteLine(headlights);
}
}
--------------------
Code VB.Net Abstract Class
--------------------
Public MustInherit Class Car
' Declare Variable value
Protected headlights As Int16 = 100 Declare Abstract Method
' Declare abstract Method
Public MustOverride Sub Move(ByVal NewX As Integer, ByVal NewY As Integer)
4
NOTE:
Abstract member is not implemented (define) in the base class and must be implemented in derived
classes
Virtual method must be implemented (define) in the base class, but may be optionally overriden in
the derived class if different behavior is required.
Example :
--------------------
Code C#
--------------------
class Hello
{
public abstract class Talk
{
public abstract void speak(); // not Implemented
4
What are Interfaces Classes?
An Interface is a reference type and it contains only non-implement (only means declare) members.
Interface's members can be Events, Methods, Properties and Indexers. Any implementation must be
placed in drive class. The interface can't contain constants, data fields, constructors, destructors and
static members. All the member declarations inside interface are implicitly public.
Interfaces in C# are provided as a replacement of multiple inheritance. Because C# does not support multiple
inheritance
Example 1:
--------------------
Code C#
--------------------
public delegate void StringListEvent(IStringList sender);
Example 2:
--------------------
Code C#
1st interface Class
--------------------
interface Icls1
{
void tag();
string Text
{ Declare methods, property
get;
set;
}
} 2nd interface Class
interface Icls2
{
void cls2_tag();
string cls2_Text
{
get;
set;
}
} Multiple inheritance through Interface
4
{ Define methods, property
Console.WriteLine("Use Interface in drive class");
}
public string Text
{
get
{
return strtextVal; Define methods, property
}
set
{
strtextVal = value;
}
}
public void cls2_tag()
{
Console.WriteLine("Use Interface in drive class");
}
public string cls2_Text
{
get
{
return strtextVal;
}
set
{
strtextVal = value;
}
}
}
--------------------
Code VB.Net
--------------------
Public Interface Icls1
Sub tag()
Property text()
End Interface
Public Interface Icls2
Sub cls2_tag()
Property cls2_text()
End Interface
4
strtextVal = value
End Set
End Property
4
Abstract class vs. Interface
Interface can only contain events, indexers, methods or properties and of these defined in interface
are by default public.
abstract; while as abstract class can contain abstract methods, abstract property as well as other
members with implementation.
An Interface can support multiple inheritance, while abstract class cannot support multiple inheritance.
An Interface can be inherited from by structures, when abstract class cannot be inherited from by
structures.
Multiple inheritance A class may inherit several interfaces. A class may inherit only one
abstract class.
Access Modfiers An interface cannot have access An abstract class can contain
modifiers for the methods, access modifiers for the
events, properties etc, everything is methods, properties etc
assumed as public
Core VS Peripheral Interfaces are used to define the An abstract class defines the
peripheral abilities of a class. In other core identity of a class and
words both Human and Vehicle can there it is used for objects of
inherit from a IMovable interface. the same type.
Fields and Constants No fields can be defined in interfaces An abstract class can have
fields and constrants defined