Inheritance and Polymorphism
Inheritance and Polymorphism
SoloLearn
It is a programming concept that allows the definition of classes based on another class.
It primarily composed of two (2) classes:
o The class whose properties are inherited by another class is called the Base class.
o The class which inherits the properties is called the Derived class.
The derived class inherits all the features from the base class, and can have its own
additional features; it essentially allows the derived class to reuse the code in the base class
without having to rewrite it.
This allows creation and maintenance of an application in C# easier.
Despite having inheritance, the base class’s constructor and destructor are NOT inherited
by any of its derived classes; therefore, it is important to also define constructors and
destructors for each individual derived class.
Invoking a derived class’s constructor or destructor (usually through an instantiated object
of the derived class) results in the constructor or destructor of a main class being
automatically invoked as well.
IV. Polymorphism
A programming concept that allows a single method to have a number of different
implementations.
Occurs when there is a hierarchy of classes and they are related through inheritance from a
common base class.
Specifically means that a call to a member method will cause a different implementation to
be executed depending on the type of object that invokes the method.
V. Abstract Classes
These are base classes that are defined when there is no meaningful need for a virtual
method to have a separate definition within the said base classes.
They allow derived classes to create virtual methods on their own while fulfilling the
purpose of a virtual method in a base class.
Uses the abstract keyword during base class and method definition; methods written within
the abstract class do not have a code body since its contents will be defined by derived
classes.
VI. Interfaces
A completely abstract class, containing only abstract members.
It is declared using the interface keyword.
All members of the interface are abstract by default, so there is no need to use the abstract
keyword.
All members of an interface are always under the public access modifier, and no other
access modifiers can be applied to them.
It is common to use the capital letter I as the starting letter for an interface name.
Interfaces can contain properties, methods, etc. but CANNOT contain fields (variables).
A. Implementing Interfaces
When a class implements an interface, it must also implement or define all of its
methods.
The term describes the process of creating a class based on an interface. The interface
simply describes what a class should do, while the class implementing the interface
must define how to accomplish the behaviors.
The syntax to implement an interface is the same as the one used for deriving classes
from a base class.
VIII. Namespaces
These are entities that declare a scope that contains a set of related objects.
They can be used to organize and personalize code elements; programmers can define their
own namespaces and use them in their programs.
The using keyword can be used to state that any part of a program is using a given
namespace.
Personalized namespaces can be used to group class and method names for larger
programming projects; this can be done by specifying a specific class as a namespace using
the namespace keyword.
References
Microsoft. (n.d.). Introducing Visual Studio.NET. Retrieved on May 8, 2018, from
http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx.
SoloLearn. (n.d.). C# tutorial. Retrieved on July 17, 2018, from
https://www.sololearn.com/Course/CSharp/