Lecture 9
Lecture 9
1. Name Equivalence
Two types are considered equivalent if they have the same name (i.e., explicitly declared as the
same type).
Example in Pascal:
pascal
CopyEdit
type
A = Integer;
B = Integer;
var
x: A;
y: B;
Here, A and B are both defined as Integer, but under strict name equivalence, x and y
are not considered the same type.
Under loose name equivalence, they might be treated as equivalent.
2. Structural Equivalence
Two types are considered equivalent if they have the same structure (i.e., identical components
and arrangement), regardless of their names.
Example in C:
c
CopyEdit
struct Point1 { int x; int y; };
struct Point2 { int x; int y; };
Here, Point1 and Point2 have the same structure, so under structural equivalence,
they are considered the same type.
In C, however, struct types are typically treated with name equivalence, meaning
Point1 and Point2 are not the same unless explicitly aliased.
Comparison Table:
Types are the same only if they have the type A = Integer; type B =
Name Equivalence
same name Integer; (A ≠ B)
1. Polymorphism
2. Overloading
3. Inheritance
1. Polymorphism
class Animal {
public:
virtual void makeSound() { // Virtual function for polymorphism
cout << "Animal makes a sound" << endl;
}
};
int main() {
Animal* a; // Pointer of base class type
Dog d;
a = &d; // Base class pointer points to derived class object
a->makeSound(); // Calls Dog's makeSound() due to polymorphism
return 0;
}
Output:
CopyEdit
Dog barks
Key Points:
2. Overloading
Overloading allows multiple methods/functions to have the same name but different
signatures (parameter lists). It occurs in two ways:
Function Overloading: Multiple functions with the same name but different parameters.
Operator Overloading: Redefining operators for user-defined types.
class Math {
public:
// Function Overloading: Same function name, different parameters
int add(int a, int b) {
return a + b;
}
int main() {
Math obj;
cout << obj.add(5, 10) << endl; // Calls int version
cout << obj.add(5.5, 2.2) << endl; // Calls double version
return 0;
}
Output:
CopyEdit
15
7.7
Key Points:
3. Inheritance
Inheritance allows a class (child) to derive properties and behavior from another class
(parent), promoting code reusability.
class Parent {
public:
void show() {
cout << "This is the Parent class" << endl;
}
};
int main() {
Child obj;
obj.show(); // Inherited function
obj.display(); // Child class function
return 0;
}
Output:
python
CopyEdit
This is the Parent class
This is the Child class
Key Points:
Summary Table
Concept Description Example
1. Polymorphism
🔹 Definition: Allows objects of different classes to be treated as objects of a common base class, enabling
method overriding and dynamic method dispatch.
✅ Advantages of Polymorphism
1. Code Reusability – Allows the same interface for different types, reducing code duplication.
2. Extensibility – New behaviors can be added with minimal modification.
3. Improves Readability & Maintainability – Easier to manage large codebases by using a common
interface.
4. Dynamic Method Invocation – Enables runtime decision-making (via virtual functions in C++).
❌ Disadvantages of Polymorphism
1. Increased Complexity – More abstract classes and interfaces make code harder to understand.
2. Performance Overhead – Runtime polymorphism (via virtual functions) introduces a slight
performance cost.
3. Difficult Debugging – Tracing overridden methods across multiple classes can be challenging.
4. Memory Overhead – Requires extra memory for vtables and pointers in languages like C++.
2. Overloading
🔹 Definition: Allows multiple functions or operators with the same name but different signatures
(different parameters or return types).
✅ Advantages of Overloading
1. Improves Code Readability – The same function name represents multiple related behaviors.
2. Enhances Code Reusability – Allows different implementations without defining multiple
function names.
3. Reduces Function Naming Conflicts – Provides intuitive function names for different parameter
types.
4. Efficient Use of Memory – The compiler determines the function at compile-time (no runtime
overhead).
❌ Disadvantages of Overloading
1. Confusion in Usage – Too many overloaded functions can make it difficult to determine the right
one.
2. Limited by Language Rules – Some languages (e.g., Python) do not support function overloading
natively.
3. Maintenance Issues – Changes to one overloaded function might require changes to others.
4. Ambiguity Errors – The compiler might struggle to determine which overloaded function to use
in some cases.
3. Inheritance
🔹 Definition: Allows a class (child) to inherit properties and behaviors from another class (parent),
promoting code reuse.
✅ Advantages of Inheritance
❌ Disadvantages of Inheritance
1. Tight Coupling – Child classes depend heavily on parent classes, making changes difficult.
2. Increased Complexity – Multiple levels of inheritance can make code harder to manage.
3. Unintended Side Effects – Changes in a base class can affect all derived classes, potentially
causing unexpected behavior.
4. Performance Overhead – Inherited methods take additional memory and processing time.
Summary Table
Concept Advantages Disadvantages
Reduces redundancy, easy extension, better Tight coupling, unintended side effects,
Inheritance
code organization performance overhead
Type parameterization is a programming language feature that allows the creation of generic types or
polymorphic functions that can operate on different data types while maintaining type safety. It enables
code reusability and abstraction.
Type parameterization means defining functions, classes, or data structures that can operate on
multiple types without specifying the exact type at the time of writing. The actual type is provided as a
parameter when the function or class is used.
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
cout << add(5, 10) << endl; // Works with integers
cout << add(5.5, 2.3) << endl; // Works with floats
return 0;
}
✅ Benefits: The same function add() works for multiple data types.
Languages like C++ (Templates) and Java (Generics) allow defining parameterized classes and methods.
java
CopyEdit
// Generic Class with Type Parameter T
class Box<T> {
private T value;
public T getValue() {
return value;
}
}
✅ Benefits: Box<T> can store any data type (Integer, String, etc.).
B. Parametric Polymorphism (Functional Languages - Haskell, Scala)
Example in Haskell
haskell
CopyEdit
-- Function that works for any type 'a'
identity :: a -> a
identity x = x
✅ Benefit: Works for any type (Int, String, etc.) without changing the function.
Code Reusability Write once, use with multiple types May increase compilation time
Flexibility Works with any compatible data type Not all languages support it
Performance No need for explicit type conversions Might introduce overhead in some cases
Type
High Medium High
Safety
Use Case Generic classes & functions Method overriding Different function signatures
5. Conclusion
Type parameterization enhances code reusability, flexibility, and type safety, making it a core feature in
modern programming languages. It is widely used in generic programming (C++, Java), functional
programming (Haskell, Scala), and databases (SQL, GraphQL).