Advance Programming
Advance Programming
Unit - 2 by Monu
Kumar
JRF NSUT Delhi
Polymorphism using Inheritance in OOP
1. What is Polymorphism?
● Definition: Polymorphism allows one interface to be used for a general class of
actions.
● Example: A function can behave differently based on the object that calls it.
Polymorphism using Inheritance in OOP
2. Key Concept: Inheritance
● Inheritance: A mechanism where one class (child class) inherits properties and
behaviors from another (parent class).
● Enables reusability and extension of code.
Polymorphism using Inheritance in OOP
3. How Polymorphism Works with Inheritance
4. Code Example:
Polymorphism using Inheritance in OOP
Polymorphism using Inheritance in OOP
Explanation:
● Definition: The process by which Java determines which method to invoke when
multiple methods with the same name exist.
● Occurs in cases of:
○ Inheritance (overriding methods)
○ Polymorphism (method calls through reference types)
Method Resolution in Java
2. Method Resolution in Inheritance
● Method Overriding:
○ Child class overrides a method from the parent class.
○ During method call, Java determines the correct method to invoke based on the
runtime object type, not the reference type
Method Resolution in Java
3. How Method Resolution Works:
● Inheritance: Allows a class to inherit properties and methods from another class.
● Interface: Defines a contract that a class must follow by implementing abstract
methods.
Interface v/s Inheritance in Java
2. Key Differences
Access Modifiers Can inherit concrete methods Cannot have fields, only
and fields. method signatures.
1. Inheritance:
Interface v/s Inheritance in Java
2. Interface:
Interface v/s Inheritance in Java
4. When to Use:
● Inheritance:
○ When there is a clear “is-a” relationship.
○ For code reuse (common properties/methods).
● Interfaces:
○ When unrelated classes need to share common behavior.
○ To achieve multiple inheritance of behavior.
Interface v/s Inheritance in Java
5. Conclusion