You are on page 1of 6

What are Abstract Classes?

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)

' Declare NON-abstract Method


Public Overridable Sub clsbaseMethod()
Console.WriteLine("Parent")
End Sub
End Class
Define Abstract Method
Public Class tool
Inherits Car
Public Overrides Sub Move(ByVal NewX As Integer, ByVal NewY As Integer)
Console.WriteLine("Child Move")
End Sub
Public Overrides Sub clsbaseMethod()
Console.WriteLine(headlights)
End Sub
End Class

4
NOTE:

1. Base class abstract method must to define in drive class.


2. An abstract class cannot be a sealed class.
3. An abstract method cannot be private.
4. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly
virtual.
5. An abstract member cannot be static.
6. The access modifier of the abstract method should be same in both the abstract class and its
derived class. If you declare an abstract method as protected, it should be protected in its
derived class. Otherwise, the compiler will raise an error.

Difference between an abstract method & virtual method:

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

public virtual void goodbye()// its implements


{
Console.WriteLine("Talk class says goodbye!");
}
}

public class SayHello : Talk


{
public override void speak()
{
Console.WriteLine("Hello!");
}
}

static void Main()


{
SayHello hello = new SayHello();
hello.speak();
hello.goodbye();
}
}

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);

public interface IStringList


{
void Add(string s); //Method
int Count { get; } //Property
event StringListEvent Changed; //Event
string this[int index] { get; set; } //Indexer
}

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

public class gcls: Icls1,Icls2


{
private string strtextVal;
public void tag()

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;
}
}
}

public class main


{
gcls objcls = new gcls();
objcls.cls2_tag();

--------------------
Code VB.Net
--------------------
Public Interface Icls1
Sub tag()
Property text()
End Interface
Public Interface Icls2
Sub cls2_tag()
Property cls2_text()
End Interface

Public Class gcls


Implements Icls1, Icls2
Private strtextVal As String
Public Sub tag() Implements Icls1.tag
Console.WriteLine("Use Interface in drive class")
End Sub

Public Property text() As Object Implements Icls1.text


Get
text = strtextVal
End Get
Set(ByVal value As Object)

4
strtextVal = value
End Set
End Property

Public Sub cls2_tag() Implements Icls2.cls2_tag


Console.WriteLine("Use Interface in drive class")
End Sub

Public Property cls2_text() As Object Implements Icls2.cls2_text


Get
cls2_text = strtextVal
End Get
Set(ByVal value As Object)
strtextVal = value
End Set
End Property
End Class

Why do we use interfaces?


We use interfaces because they allow reusability of code as well as help create useful relationships between objects.

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.

Feature Interface Abstract class

Multiple inheritance A class may inherit several interfaces. A class may inherit only one
abstract class.

Default An interface just defines a contract, it An abstract class can provide


implementation cannot provide any implementation. complete, default code and/or
just the details that have to be
overridden.

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.

Homogeneity If various implementations only share If various implementations are


method signatures then it is better to of the same kind and use
use Interfaces. common behaviour or status
then abstract class is better to
use.

Adding functionality If we add a new method to an If we add a new method to an


(Versioning) Interface then we have to track down abstract class then we have
all the implementations of the the option of providing default
interface and define implementation implementation and therefore
for the new method. all the existing code might
work properly.

Fields and Constants No fields can be defined in interfaces An abstract class can have
fields and constrants defined

You might also like