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

Advance Programming

NSUT java notes

Uploaded by

xamidi3633
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Advance Programming

NSUT java notes

Uploaded by

xamidi3633
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

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

● A base class defines a common interface.


● Derived classes implement or override methods.
● A parent class reference can point to child class objects.
Polymorphism using Inheritance in OOP

4. Code Example:
Polymorphism using Inheritance in OOP
Polymorphism using Inheritance in OOP
Explanation:

● Animal is the base class.


● Dog and Cat are derived classes that override the sound() method.
● The base class reference (Animal) can point to objects of derived classes (Dog, Cat).
● Polymorphism occurs when the method is called based on the object type, not the
reference type.
Polymorphism using Inheritance in OOP
5. Benefits of Polymorphism

● Flexibility: Use a common interface to handle different types.


● Scalability: Easily extend with new types without altering existing code.
Method Resolution in Java
1. What is Method Resolution?

● 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:

1. Static Binding (Compile-time):


○ Decides method signature (name, parameters) during compilation.
○ Happens with method overloading.
2. Dynamic Binding (Runtime):
○ Decides which version of the method (parent or child) to call based on the actual
object type during runtime.
○ Involves method overriding.
Method Resolution in Java
4. Code Example:
Method Resolution in Java
5. Key Points:

● Compile-time binding: For method overloading (same name, different parameters).


● Runtime binding: For method overriding (same name and parameters, different
implementation).
● Object type determines which overridden method is invoked.
Interface v/s Inheritance in Java
1. Overview

● 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

Aspects Inheritance Interface

Purpose To inherit features (methods To specify behaviors (methods)


and fields). that must be implemented

Keyword extends implements

Multiple Inheritance Not allowed (a class can Allowed (a class can


extend only one class). implement multiple interfaces).

Access Modifiers Can inherit concrete methods Cannot have fields, only
and fields. method signatures.

Method Implementation Inherited methods can be Methods must be implemented


overridden. in the class.
Interface v/s Inheritance in Java
3. Use Case Examples

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

● Use inheritance for hierarchical relationships and code reuse.


● Use interfaces for defining contracts across different, unrelated classes.

You might also like