Oops
Oops
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:
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.
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:
Multiple inheritance A class may inherit several A class may inherit only one
interfaces. abstract class.
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.
Fields and Constants No fields can be defined in An abstract class can have
interfaces fields and constants defined