Flashcards
Polymorphism
Cards in this deck (10)
- 1What does Polymorphism means?many forms
- 2WWhen a single entity behaves in different ways
- 3Types of Polymorphism- Compile Time (Static) Polymorphism
-> through Method Overloading or Constructor Overloading
- Runtime (Dynamic) Polymorphism
-> through Method Overriding - 4Compile-time polymorphism- Behavior gets resolved during Java compilation
- Also known as Method overloading
- Same method exhibits different behavior with based on the parameters passed. - 5What is Method OverloadingMethod overloading allows developers to have one or more methods the same name if and only if parameter lists or sequence of the parameters are different
- 6What is Constructor overloadingConstructor overloading is also possible in Java by varying the type of parameters passed or the sequence of parameters passed to the constructor.
- It is a way to achieve Polymorphism - 7Rules for Method Overloading-Method Signature
One or more methods in Java can have same name by altering the method signature which compromises:
1. number of arguments
2. types of arguments
3. order of arguments if they are of different types.
- Method Return Type
Does not have major say in method signature, so just changing the return type will not make a legal method overload. - 8Run-Time or Dynamic Polymorphism- Run-time Polymorphism refers to behavior that is resolved during the execution phase of Java code
- Also referred to as Method overriding
- A child classes can customize the behavior of method already defined in a parent class.
The call to which version of the method to be executed(childs or parents version) is decided by the JVM based on the object using which method is invoked. - 9Method Overriding- Method Overriding in Java mandates to have is-a relationship between parent- child classed
- Parent and Child classes have same method name with same signature including return-types - 10Rules for Method Overriding- Location -> A method can only be overridden in sub-class, not in the same class
- Visibility -> The overriding method can not reduce access of overridden method
- Accessibilty -> Overriding method can increase access of overridden method
- Types of Methods -> The methods marked with private, static, and final are not eligble for overriding
-Return Type -> The return type of overriding method must be the same as overridden method
Information
- Description
- Flashcards on Polymorphism, created by BLU BLU on 24/01/2025.
- Created by
- BLU BLU
- Views
- 0
- Rating