0% found this document useful (0 votes)
2 views3 pages

Netke Oop2

The document discusses runtime polymorphism, which allows method calls to be resolved at runtime through inheritance and method overriding. It highlights the benefits of flexibility, extensibility, and decoupling in code design, along with examples of its implementation. The conclusion emphasizes the importance of runtime polymorphism in creating adaptable and maintainable software systems.

Uploaded by

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

Netke Oop2

The document discusses runtime polymorphism, which allows method calls to be resolved at runtime through inheritance and method overriding. It highlights the benefits of flexibility, extensibility, and decoupling in code design, along with examples of its implementation. The conclusion emphasizes the importance of runtime polymorphism in creating adaptable and maintainable software systems.

Uploaded by

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

Department of _____________________Engineering

Academic Year : 2018-19


SUBJECT: Object Oriented Programming
CLASS: S.Y.B.Tech SEMESTER: III
ASSIGNMENT NO.: 2 DATE OF SUBMISSION: 01/12/23
NAME OF STUDENT: RUSHIKESH NETKE ROLL NO.: B-90
TOPIC: RUN TIME POLYMORPHISM
WEBSITE URL REFERRED:

Summary/Abstract/Review:

Key Points:
 Definition: Runtime polymorphism, also known as dynamic polymorphism or
late binding, allows the decision of which method to call to be made at runtime
rather than compile time.
 Mechanism: It's achieved through a combination of inheritance and method
overriding.
 Essential for:
o Flexible code that can adapt to different object types at runtime.
o Implementing design patterns like the Strategy pattern or the Template
Method pattern.
How It Works:
1. Virtual Functions: In the base class, mark a method as virtual using
the virtual keyword (in C++ and some other languages).
2. Overriding: Derived classes can override virtual methods, providing their own
implementation.
3. Method Calls: When a virtual method is called through a base class pointer or
reference, the actual method invoked is determined by the object's type at
runtime, not the reference type.
Example:

class Animal {
public:
virtual void makeSound() {
std::cout << "Generic animal sound\n";
}
};

class Dog : public Animal {


public:
void makeSound() override {
std::cout << "Woof!\n";
}
};
Department of _____________________Engineering
class Cat : public Animal {
public:
void makeSound() override {
std::cout << "Meow!\n";
}
};

void makeAnimalSound(Animal& animal) {


animal.makeSound(); // Calls the appropriate method at runtime
}

Benefits:
 Flexibility: Code can work with objects of different types without explicit
checks.
 Extensibility: New derived classes can be added without modifying existing
code.
 Decoupling: Objects can interact with each other based on their interfaces, not
their concrete types.
Common Use Cases:
 Implementing common interfaces for diverse objects (e.g., graphical shapes, file
system objects).
 Creating libraries with reusable components that can be customized by users.
 Building adaptable and maintainable software systems.

Conclusion:

Runtime polymorphism delivers flexibility and elegance:


 Adapts on the fly: Handles different object types dynamically, for
clean, reusable code.
 Chooses the right path: Selects the correct method based on the actual
object, enabling open-ended systems.
 Boosts code reuse: Promotes shared interfaces and subclass-specific
implementations.
 Elevates abstraction: Treats objects based on shared features, simplifying
program logic.
Used smartly, it's a powerful tool for building flexible, maintainable software.

Name & Sign of Subject In-charge: Marks:


Department of _____________________Engineering

You might also like