1. The purpose of abstract class is to provide default
functionality to its sub classes.
2. When a method is declared as abstract in the base
class then every derived class of that class must provide its own definition
for that method.
3. An abstract class can also contain methods with
complete implementation, besides abstract methods.
4. When a class contains at least one abstract method,
then the class must be declared as abstract class.
5. It is mandatory to override abstract method in
the derived class.
6. When a class is declared as abstract class, then it is
not possible to create an instance for that class. But it can be used as a
parameter in a method.
Abstract class in c# example
The following example creates three classes
shape, circle and rectangle where circle and rectangle are inherited from the
class shape and overrides the methods Area() and Circumference() that are
declared as abstract in Shape class and as Shape class contains abstract
methods it is declared as abstract class.
using System;
namespace ProgramCall
{
//Abstract class
abstract class Shape1
{
protected float R, L, B;
//Abstract methods can have only declarations
public abstract float Area();
public abstract float Circumference();
}
class
Rectangle1 : Shape1
{
public void GetLB()
{
Console.Write("Enter Length
: ");
L = float.Parse(Console.ReadLine());
Console.Write("Enter Breadth : ");
B = float.Parse(Console.ReadLine());
}
public override float Area()
{
return L * B;
}
public override float Circumference()
{
return 2 * (L + B);
}
}
class
Circle1 : Shape1
{
public void GetRadius()
{
Console.Write("Enter Radius
: ");
R = float.Parse(Console.ReadLine());
}
public override float Area()
{
return 3.14F * R * R;
}
public override float Circumference()
{
return 2 * 3.14F * R;
}
}
class
MainClass
{
public static void Calculate(Shape1 S)
{
Console.WriteLine("Area : {0}", S.Area());
Console.WriteLine("Circumference : {0}", S.Circumference());
}
static void Main()
{
Rectangle1 R = new Rectangle1();
R.GetLB();
Calculate(R);
Console.WriteLine();
Circle1 C = new Circle1();
C.GetRadius();
Calculate(C);
Console.Read();
}
}
}
Output
Enter Length:
10
Enter Breadth: 12
Area: 120
Circumference: 44
Enter Radius:
5
Area: 78.5
Circumference: 31.4
Polymorphism
·
Compile
time polymorphism:
1. Static polymorphism is also called as Compile
Time polymorphism.
2. In
Static polymorphism methods are overloaded
with same name but having different signatures.So it is called as method
overloading.
3. It is a compile time polymorphism because compiler already knows
what type object it is linking to, what are the methods it is going to call it.
So linking a method during a compile time also called as Early binding.
·
Runtime
Polymorphism:
1. In Dynamic polymorphism methods are overridden
so it also called as method overriding.
2. During run time, Method overriding can be
achieved by using inheritance principle and using "virtual" and
"override" keyword. so this type of polymorphism can also be called
as late binding.
3. In late binding compiler doesn't know what
kind of methods it has to call and which can be achieved only during the run
time. so it is called as run time polymorphism.
Interface
1. An interface is not a class. An interface has
no implementation;
2. it only has the signature or in other words,
just the definition of the methods without the body. As one of the similarities
to Abstract class, it is a contract that is used to define hierarchies for all
subclasses or it defines specific set of methods and their arguments.
3. The main difference between them is that a
class can implement more than one interface but can only inherit from one
abstract class. Since C# doesn’t support multiple inheritance, interfaces are
used to implement multiple inheritance.
class
Demo
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
}
interface
abc
{
}
Output
Hello
Interfaces
The
above program compiles and runs successfully to produce the desired output. The
above program consists of a class Demo and within it an entry point function
Main() that prints Hello Interfaces. The above program also defines an
interface abc. Interface abc is empty at this point of time. Let's add some
elements to this interface.
P2.cs
class
Demo
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
}
interface
abc
{
int x;
}
Output
P2.cs(11,3):
error CS0525: Interfaces cannot contain fields
Error!
Interfaces in C# cannot contain fields i.e variables. The above program
declared an integer variable x in the interface abc. And that's what hit the C#
compiler badly.
P3.cs
class Demo
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
}
interface
abc
{
void xyz()
{
System.Console.WriteLine("In
xyz");
}
}
Output
P3.cs(11,8):
error CS0531: 'abc.xyz()': interface members cannot have a definition
This
time over we included a function xyz() inside the interface. It told us that
interface members cannot have a definition.
P4.cs
class
Demo
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
}
interface
abc
{
void xyz();
}
Output
Hello
Interfaces
The
above program compiles and runs successfully to produce the desired output.
Interfaces in C# can only contain function declarations.
P4.cs
class
Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
}
interface
abc
{
void xyz();
}
Output
P4.cs(1,7):
error CS0535: 'Demo' does not implement interface member 'abc.xyz()'
P4.cs(11,8):
(Location of symbol related to previous error)
Well,
in the above program class Demo did marry the interface abc through the line
class demo : abc but as usual there's a small misunderstanding between the
newlyweds. Class Demo needs to take the responsibility of defining the
functions whose prototypes are declared by the marrying interface abc. Since
class Demo in the above program has not been implemented i.e. defined the
function xyz whose prototype is declared by the marrying interface abc we get
an error in the above program. To fix this issue, the class Demo has to take
the responsiility of defining the function xyz whose prototype is declared by
the marrying interface abc. And that is what you get to see in the following
program.
P5.cs
class
Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
}
void xyz()
{
System.Console.WriteLine("In
xyz");
}
}
interface
abc
{
void xyz();
}
Output
a.cs(1,7):
error CS0536: 'Demo' does not implement interface member
'abc.xyz()'.'Demo.xyz()' is either static, not public, or has the wrong return type.
a.cs(16,8):
(Location of symbol related to previous error)
a.cs(7,8):
(Location of symbol related to previous error)
Error
again! It's not enough for the class Demo to implement the function xyz. It has
to impress the bride interface abc by declaring its implementation of xyz as
public. And that's what is done by the following program.
P6.cs
class
Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
xyz();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
}
//-----------------
interface
abc
{
void xyz();
}
Output
Hello
Interfaces
In
xyz
Bingo!
The above program compiles and runs successfully to produce the desired output.
As mentioned earlier using interfaces we can invoke functions from different
classes using the same interface reference. For this, we need to have different
classes to implement the same interface. In the above program our class Demo is
implementing the interface abc. Let's have another class Sample that implements
the interface abc.
----------------------------------------------------------------------------------------------------------
P7.cs
class
Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
refDemo.xyz();
Sample refSample = new Sample();
refSample.xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo ::
xyz");
}
}
interface
abc
{
void xyz();
}
class
Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample
:: xyz");
}
}
Output
In
Demo :: xyz
In
Sample :: xyz
The
above program compiles and runs successfully to produce the desired output.
refDemo is a reference to the object of class Demo. refSample is a reference to
the object of class Sample. Both the classes implement the interface abc and
hence define their own implementation of the function xyz(). From within the
entry point function Main() xyz() of the respective classes Demo and Sample are
invoked through references refDemo and refSample.
----------------------------------------------------------------------------------------------------------
Now
that we have two different classes implementing the same interface its time to
show you how to invoke functions from different classes using the same
interface reference.
P8.cs
class
Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
abc refabc = new Demo();
refabc.xyz();
abc
refabc = new Sample();
refabc.xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo ::
xyz");
}
}
interface
abc
{
void xyz();
}
class
Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample
:: xyz");
}
}
Output
In
Demo :: xyz
In
Sample :: xyz
The
above program compiles and runs successfully to produce the desired output.
Inside Main() we have an interface reference refabc of type interface abc.
Reference of object of class Demo is stored in refabc and xyz() of class Demo
is invoked using refabc. Next, the reference of object of class Sample is
stored in refabc and xyz() of class Sample is invoked using refabc. Thus, we
were able to invoke xyz() that belongs to different classes Demo and Sample via
a common interface reference refabc.
----------------------------------------------------------------------------------------------------------
The
following program uses a for loop to invoke the functions of different classes
Demo and Sample that implement the same interface "interface abc"
using a single interface reference refabc whose type matches the interface
"interface abc" which the classes impliment.
P9.cs
class
Demo : abc
{
public static void Main()
{
abc [] refabc = {new Demo(), new Sample()}
;
for (int i = 0; i<= 1; i++)
refabc[i].xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo ::
xyz");
}
}
interface
abc
{
void xyz();
}
class
Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample
:: xyz");
}
}
Output
In
Demo :: xyz
In
Sample :: xyz
The
above program compiles and runs successfully to produce the desired output.
refabc is an array of type interface abc. It stores the references to objects
of classes Demo and Sample. In the for loop, using the array refabc, we are
invoking the function xyz() of class Demo and Sample. A class can impliment as
many interfaces as it wants. Take the following program.
----------------------------------------------------------------------------------------------------------
P10.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
abc refabc = new Demo();
refabc.xyz();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
public void pqr()
{
System.Console.WriteLine("In
xyz");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void pqr();
}
Output
Hello
Interfaces
In
xyz
The
above program compiles and runs successfully to produce a desired output. Class
Demo implements interface abc and thereby function xyz(). Class Demo also
impliments interface def and thereby function pqr(). ref abc which is a
variable of type Interface abc, refers to object of class Demo. Next xyz() of Demo
is invoked via refabc as refabc is a variable of type Interface abc which
contains the prototype for function xyz().
----------------------------------------------------------------------------------------------------------
P11.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
abc refabc = new Demo();
refabc.xyz();
refabc.pqr();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
public void pqr()
{
System.Console.WriteLine("In
xyz");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void pqr();
}
Output
P11.cs(9,5):
error CS0117: 'abc' does not contain a definition for 'pqr'
Error!
An attempt to invoke pqr() of Demo via refabc fails as refabc is a variable of
type Interface abc which contains the prototype for function xyz() and NOT
pqr(). One can invoke pqr() of Demo via a reference variable of type Interface
def as the interface def contains the prototype for function pqr(). And that's
what is done by the following program.
----------------------------------------------------------------------------------------------------------
P12.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.pqr();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
public void pqr()
{
System.Console.WriteLine("In
pqr");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void pqr();
}
Output
Hello
Interfaces
In
xyz
In
pqr
The
above program compiles and runs successfully to produce a desired output. Class
Demo impliments the interfaces abc and def. An object of class Demo is created
and its reference is stored in refDemo. refabc which is a variable of type
Interface abc, refers to the object of class Demo. Next xyz() of Demo is
invoked via refabc as refabc is a variable of type Interface abc which contains
the prototype for function xyz(). Similarly, refdef which is a variable of type
Interface def, refers to object of class Demo. Next pqr() of Demo is invoked
via refdef as refdef is a variable of type Interface def which contains the
prototype for function pqr().
----------------------------------------------------------------------------------------------------------
P13.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void xyz();
}
Output
Hello
Interfaces
In
xyz
In
xyz
The
above program compiles and runs successfully to produce a desired output. Both
the interfaces abc and def declare the prototypes for function xyz(). Class
Demo implements interfaces abc as well as def and defines the function xyz() as
well. Thus we can invoke the function xyz() through either of the interface
reference variables (refabc or refdef) after storing the reference to the
object of class Demo in refabc or refdef. This poses a question, how can we
have an implementation of xyz that is specific to interface abc and
implementation of xyz that is specific to def inside class Demo? Well, for this
we need to use the fully qualified names as in the following program.
----------------------------------------------------------------------------------------------------------
P14.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
public void abc.xyz()
{
System.Console.WriteLine("In
abc.xyz");
}
public void def.xyz()
{
System.Console.WriteLine("In
def.xyz");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void xyz();
}
Output
a.cs(13,15):
error CS0106: The modifier 'public' is not valid for this item
a.cs(18,15):
error CS0106: The modifier 'public' is not valid for this item
Bummer!
We used the fully qualified name and we got an error. That's because when we
use fully qualified names for functions whose prototypes are a part of
interfaces, the compiler doesn't need decorators like public. So we decided to
remove the access specifier public from the above program.
----------------------------------------------------------------------------------------------------------
P15.cs
class
Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
void abc.xyz()
{
System.Console.WriteLine("In
abc.xyz");
}
void def.xyz()
{
System.Console.WriteLine("In
def.xyz");
}
}
interface
abc
{
void xyz();
}
interface
def
{
void xyz();
}
Output
Hello
Interfaces
In
abc.xyz
In
def.xyz
The
above program compiles and runs successfully to produce a desired output. A
fully qualified naming system allows us to define interfaces having same
function prototypes. In the above example, interface abc and def contain the
same function prototypes for function xyz(). Class Demo impliments both the
interfaces. Using fully qualified names, it defines implementation of xyz that
is specific to interface abc and implementation of xyz that is specific to
interface def.
----------------------------------------------------------------------------------------------------------
P16.cs
class
Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
public void xyz()
{
System.Console.WriteLine("In
xyz");
}
public void pqr()
{
System.Console.WriteLine("In
pqr");
}
}
interface
abc
{
void xyz();
}
interface
def : abc
{
void pqr();
}
Output
Hello
Interfaces
In
xyz
In
pqr
The
above program compiles and runs successfully to produce a desired output.
Interfaces support inheritance. Interface def inherits prototypes from
interface abc. Class Demo implements the interface def. Interface variable
refdef stores the reference to object of class Demo. Functions xyz() and pqr()
of class Demo are invoked through interface reference variable refdef.
----------------------------------------------------------------------------------------------------------
P17.cs
class
Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
void def.xyz()
{
System.Console.WriteLine("In
xyz");
}
void def.pqr()
{
System.Console.WriteLine("In
pqr");
}
}
interface
abc
{
void xyz();
}
interface
def : abc
{
void pqr();
}
Output
P17.cs(12,8):
error CS0539: 'def.xyz' in explicit interface declaration is not a member of
interface
P17.cs(29,11):
(Location of symbol related to previous error)
P17.cs(1,7):
error CS0535: 'Demo' does not implement interface member 'abc.xyz()'
P17.cs(26,8):
(Location of symbol related to previous error)
Bummer!
The prototype of function xyz is an original member of interface abc. Thus,
even if interface def inherits from the interface abc, the fully qualified name
of the function xyz() remains as abc.xyz and not def.xyz as done in the above
program. In fact, we got a compiler error. This can be fixed by using the
correct fully qualified name of function xyz() as shown in the following
program.
-----------------------------------------------------------------------------------------------------------
P18.cs
class
Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
void abc.xyz()
{
System.Console.WriteLine("In xyz");
}
void def.pqr()
{
System.Console.WriteLine("In
pqr");
}
}
interface
abc
{
void xyz();
}
interface
def : abc
{
void pqr();
}
Output
Hello
Interfaces
In
xyz
In
pqr
The
above program compiles and runs successfully to produce a desired output. But
there are some pitfalls when using the fully qualified function names.
-----------------------------------------------------------------------------------------------------------
P19.cs
class
Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello
Interfaces");
Demo refDemo = new Demo();
refDemo.xyz();
refDemo.pqr();
}
void abc.xyz()
{
System.Console.WriteLine("In
xyz");
}
void def.pqr()
{
System.Console.WriteLine("In
pqr");
}
}
interface
abc
{
void xyz();
}
interface
def : abc
{
void pqr();
}
Output
P19.cs(7,5):
error CS0117: 'Demo' does not contain a definition for 'xyz'
P19.cs(8,5):
error CS0117: 'Demo' does not contain a definition for 'pqr'
The
above program fails to clear the compilation hurdle. refDemo refers to an
object of class Demo. We are invoking xyz() using refDemo(). For C#, xyz() and
ddd.xyz() are two different things. It knows that abc.xyz() exists. It also
knows that just xyz() is nowhere in the class. That's why we get an error which
says Class Demo does not contain a definition of xyz(). For C#, abc.xyz() can
be invoked by an interface reference of type abc only. Similarly, def.pqr() can
be invoked by an interface reference of type def only.
·
Difference between
Abstract and Interfaces
Abstract Class:
1. Abstract Class Can contain
Abstract Methods and Non- Abstract Methods.
2. Abstract class can contain
access modifier.
3. When a Non-Abstract Class
is Inherited from an Abstract Class, the Non-Abstract Class should provide all
the implementations for the inherited Abstract Method.
Interface:
1. Interface is nothing but
Pure Abstract Class ie Interface can contain only the function declaration.
2.
All the members of the interface are Public by Default and you cannot provide
any access modifiers.