0% found this document useful (0 votes)
57 views

Inheritance and Polymorphism

The document discusses key concepts in object-oriented programming such as inheritance, polymorphism, abstract classes, and interfaces. Inheritance allows a derived class to inherit properties from a base class without rewriting code. Polymorphism allows a method to have multiple implementations depending on the object type. Abstract classes define common behaviors for derived classes to implement, while interfaces only contain abstract members that implemented classes must define.

Uploaded by

Mckinley Quinto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Inheritance and Polymorphism

The document discusses key concepts in object-oriented programming such as inheritance, polymorphism, abstract classes, and interfaces. Inheritance allows a derived class to inherit properties from a base class without rewriting code. Polymorphism allows a method to have multiple implementations depending on the object type. Abstract classes define common behaviors for derived classes to implement, while interfaces only contain abstract members that implemented classes must define.

Uploaded by

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

SH1803

Inheritance and Polymorphism


I. Inheritance

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.

II. Protected Members

A. The protected Access Modifier


 A special access modifier that is similar to the private access modifier.
 It is unique in that it can only be accessed within derived classes.
 Attempting to access any code with the modifier outside of a derived class will
encounter an error.

B. The sealed Access Modifier


 Another special access modifier that prevents a class from being inherited by other
classes or any of its members.
 It works for both base and derived classes; derived classes with the modifier cannot be
derived by other classes themselves.
 This provides a level of protection to a class so that its values cannot easily be called or
changed.

III. Derived Class Constructors and Destructors


 Constructors and destructors are special methods that are called when a class is created
and deleted respectively:
o Constructor method code usually sport the public access modifier is written with the
modifier, the identifier of the class it is based, and finally a pair of parentheses (( )).
o Destructor method code simply uses a tilde (~) before the name of the class identifier,
followed by a pair of parentheses (( )).

06 Handout 1 *Property of STI


[email protected] Page 1 of 3
SH1803

 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.

A. The virtual Keyword


 It is a keyword used to denote that a method can be leveraged for polymorphism via
overriding.
 This is usually written as part of a method declaration of a base class.

B. The override Keyword


 It is a keyword used by derived classes to override a method from their base class.
 This is usually written as part of the method declaration within the derived class’s
code block.

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.

A. Features of an Abstract Class


 An abstract class CANNOT be instantiated.
 An abstract class MAY contain abstract methods and accessors.
 A non-abstract class derived from an abstract class MUST include actual
implementations of all inherited abstract methods and accessors.

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

06 Handout 1 *Property of STI


[email protected] Page 2 of 3
SH1803

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.

VII. Nested Classes


 Simply, a class that is a member of another class.
 Demonstrates the real-life behavior of objects or data being part of a larger object or
collection of data.
 Nested classes can be used similarly to other members of the parent class.
 It can have the same access modifiers as its other member classes.

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/

06 Handout 1 *Property of STI


[email protected] Page 3 of 3

You might also like