07 Inheritance and Polymorphism
07 Inheritance and Polymorphism
The following statement is the general syntax for inheriting a class in C#:
access_modifier class DerivedClass : BaseClass {
//class members here...
}
The colon (:) symbol indicates that the class DerivedClass inherits the members of the class BaseClass. The derived classes do
not inherit the base class' constructors, but these derived classes can invoke (or call) the constructors of the base class. The
following programs show how to declare inheritance:
Example 1. Student cl a s s i nheri ts from Person cl a s s
BASE CLASS DERIVED CLASS
using System; using System;
namespace PersonNamespace { namespace PersonNamespace {
public class Person { public class Student : Person {
private string full_name; private string program;
public void setName() { public void EnrollProgram() {
this.full_name = "no name"; this.setName();
} this.program = "not enrolled";
}
public void Walk() {
Console.WriteLine("Walking..."); public void ComputeGrade() {
} Console.WriteLine("Computing grade...");
} }
} }
}
In the given example, the derived class Student inherits from the base class Person, including the field name and behavior. The
Student class can now have access to the members of the Person class, such as the method setName(), and can be invoked
using the this keyword since the methods of the derived class inherited the base class. The Person and Student classes are
both members of the namespace PersonNamespace. This is used to help in organizing the classes.
A derived class can inherit the private members of a base class, but these are not accessible. In Example 1, the instance variable
full_name of Person class is private. When the class Student class inherits the members of Person class, it will inherit the
field full_name, but it cannot access this variable. For example, when the statement this.full_name = "no name"; is
declared in the class Student, it will cause an error since the field full_name is private. When an instance variable of a base
class is declared as private, the derived class can only access that variable through methods or by using properties. The variable
07 Handout 1 *Property of STI
[email protected] Page 1 of 7
IT1907
full_name in Example 1 can only be accessed or initialized using the method setName(). Declaring the members of a superclass
as private prevents its subclasses from modifying the members of the superclass.
Protected Members of a Class
Unlike private, the protected members of a base class
are inherited and are accessible by their derivedclasses.
The keyword protected is used to declare a protected
member. Members of a superclass are declared
protected if you want its subclasses to access its
members. Figure 2 illustrates a UML diagram with
protected members. Example 2 shows the example
programs of how to declare use protected members in
inheritance.
OUTPUT
Name: Jack Paul
Age: 18
Student ID: 20191001
Program: BSCS
In the base class Person of Example 3, the constructor Person(string name, int age) is defined and initializes its instance
variables full_name and age. In the derived class, the constructor Student(string name, int age, long id, string
program) with base(name, age) is used for calling the base class constructor Person(string name, int age) to initialize
the inherited instance variables full_name and age of class Student with the values from the parameter of its constructor.
Then, the constructor of Student will initialize the instance variables student_id and student_program. The base keyword
can also be used to call a method from the base class. For example, there is base.displayInfo();.
When creating an instance of the derived class Student using the defined constructor, the required arguments of the
constructor must be defined. In the example, the parameterized constructor of Student creates a new instance using the
statement Student s1 = new Student("Jack Paul", 18, 20191001, "BSCS");, where the defined arguments match the
required parameters of the constructor.
Method Overriding
A derived class inherits the methods of its base class. Declaring a
method in a derived class with the same name as the method from
its base class is called method overriding. Method overriding is
redefining the functionality of an existing method. This is used for
defining a specific behavior to the overriding method of a derived
class that is different from the existing method from its base class.
For example, the derived class CheckingAccount in Figure 4
overrides the method withdraw from the base class BankAccount.
The method withdraw (overridden method) of BankAccount takes the
amount to withdraw then subtracts it from the current balance.
Consequently, the method withdraw (overriding method)
of CheckingAccount has a different behavior. This method adds
a withdrawal fee on the amount to withdraw before subtracting
it from the current balance.
Figure 4. Inheritance with overridden and overriding method The override method should have the same method signature
(access modifier, return type, method name, and parameter list) as
the overridden method.
In C#, the overridden method from the base class should be declared as virtual. If the class is abstract, the overridden
method should be declared abstract or virtual. The virtual modifier specifies that a derived class can override the method
in the base class. When overriding a virtual method from the base class, the override modifier is required to modify the
abstract or virtual implementation of the inherited method and must have the same method signature as the overridden
method (virtual method). Only an abstract and virtual method can be overridden in C#. The following programs demonstrate
how to declare a virtual method and create an overriding method, including how to declare and access properties.
Example 4. CheckingAccount cl a s s i nheri ts from BankAccount cl a s s with a n overri de method a nd Program cl a s s with Main method
BASE CLASS
namespace BankAccountNamespace {
public class BankAccount {
private long account_number;
private double current_balance;
private double withdrawal_amount;
public BankAccount(long account, double balance) {
this.account_number = account;
this.current_balance = balance;
In the example above, the base class BankAccount is declared as an abstract, which contains the abstract method deposit. The
derived class SavingsAccount is required to implement deposit and provide its method body and behavior. The derived class
may declare its own instances and methods, such as addInterest.
Polymorphism
Polymorphism, which means “multiple forms,” is one of the fundamental concepts of object-oriented programming.Itenables
classes to provide multiple methods with the same name but with different implementations or behavior. Polymorphism has
two (2) different forms: compile time and runtime polymorphisms.
• Compile time polymorphism – Also known as “static polymorphism,” this polymorphism is implemented using method
overloading. In method overloading, a method is executed depending on the number and type of parameters passed to it.
When a program is compiled, the compiler binds the appropriate method to the object based on the method’s arguments.
This process is called early binding.
• Runtime polymorphism – This polymorphism is a process in which the compiler determines which method to call during
runtime. This process is also called dynamic polymorphism or late binding. This is achieved using method overriding.
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program, (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.