0% found this document useful (0 votes)
25 views3 pages

Oops

An abstract class cannot be instantiated and is intended to provide a common definition for derived classes to implement. Abstract classes can contain both implemented and abstract methods, while abstract methods must be implemented in derived classes. An interface only contains method signatures without implementation and a class can implement multiple interfaces but only inherit from one abstract class.

Uploaded by

Akash K Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Oops

An abstract class cannot be instantiated and is intended to provide a common definition for derived classes to implement. Abstract classes can contain both implemented and abstract methods, while abstract methods must be implemented in derived classes. An interface only contains method signatures without implementation and a class can implement multiple interfaces but only inherit from one abstract class.

Uploaded by

Akash K Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

An abstract class cannot be instantiated.

The purpose of an abstract class is to provide a common


definition of a base class that multiple derived classes can share. For example, a class library may define
an abstract class that is used as a parameter to many of its functions, and require programmers using that
library to provide their own implementation of the class by creating a derived class.

Abstract methods have no implementation, so the method definition is followed by a semicolon instead
of a normal method block. Derived classes of the abstract class must implement all abstract methods.
When an abstract class inherits a virtual method from a base class, the abstract class can override the
virtual method with an abstract method.

If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A
class inheriting an abstract method cannot access the original implementation of the method—in the
previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can
force derived classes to provide new method implementations for virtual methods.

A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed
classes are primarily used to prevent derivation. Because they can never be used as a base class, some
run-time optimizations can make calling sealed class members slightly faster.

A class member, method, field, property, or event, on a derived class that is overriding a virtual member of
the base class can declare that member as sealed. This negates the virtual aspect of the member for any
further derived class. This is accomplished by putting the sealed keyword before the override keyword in
the class member declaration.

A struct type is a value type that is typically used to encapsulate small groups of related variables, such
as the coordinates of a rectangle or the characteristics of an item in an inventory. The following example
shows a simple struct declaration:

public struct Book


{
public decimal price;
public string title;
public string author;
}

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events,
and nested types, although if several such members are required, you should consider making your type a
class instead.

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct
members cannot be declared as protected.
1.1 Background

An Abstract class without any implementation just looks like an Interface; however there
are lot of differences than similarities between an Abstract class and an Interface. Let's ex-
plain both concepts and compare their similarities and differences.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is
why we need a class that cannot be instantiated? An abstract class is only to be sub-classed
(inherited from). In other words, it only allows other classes to inherit from it but cannot be
instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In
simple words, it is a kind of contract that forces all the subclasses to carry on the same hi-
erarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface
has no implementation; 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. 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.

Both Together

When we create an interface, we are basically creating a set of methods without any imple-
mentation that must be overridden by the implemented classes. The advantage is that it
provides a way for a class to be a part of two classes: one from inheritance hierarchy and
one from the interface.

When we create an abstract class, we are creating a base class that might have one or more
completed methods but at least one or more methods are left uncompleted and de-
clared abstract. If all the methods of an abstract class are uncompleted then it is same as
an interface. The purpose of an abstract class is to provide a base class definition for how a
set of derived classes will work and then allow the programmers to fill the implementation in
the derived classes.
There are some similarities and differences between an interface and an abstract class that I have
arranged in a table for easier comparison:

Feature Interface Abstract class

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

Default implementa- An interface cannot provide An abstract class can provide


tion any code, just the signature. complete, default code and/
or just the details that have
to be overridden.

Access Modifiers An interface cannot have ac- An abstract class can contain
cess modifiers for the subs, access modifiers for the
functions, properties etc ev- subs, functions, properties
erything is assumed as public

Core VS Peripheral Interfaces are used to define An abstract class defines the
the peripheral abilities of a core identity of a class and
class. In other words both there it is used for objects of
Human and Vehicle can in- the same type.
herit from a IMovable inter-
face.

Homogeneity If various implementations If various implementations


only share method signatures are of the same kind and use
then it is better to use Inter- common behavior or status
faces. then abstract class is better
to use.

Speed Requires more time to find Fast


the actual method in the cor-
responding classes.

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


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

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

You might also like