Lecture 6 - Polymorphism

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

8 Terms

1
New cards

What is polymorphism?

Enables a single interface (method or type) to represent multiple types. This is achieved through Method Overloading, Method Overriding, Upcasting/Downcasting.

2
New cards

What are the benefits of polymorphism?

Reusability, flexibility and maintainability.

3
New cards

What is method overloading?

Compile-time polymorphism, where o   you use the same method name with different parameters for different constructors (in the same class). Avoid overloading with nearly identical parameter lists.

4
New cards

What is Method overriding?

Run-time polymorphism, where you redefine a superclass method in a subclass, determined at run time through the @Override keyword. This is basically just inheritance!

5
New cards

What is upcasting?

Assigns a subclass object to a superclass reference.

Animal a = new Dog(); //Upcasting
a.makeSound(); //Calls the dog’s version at runtime.

6
New cards

What is downcasting?

Converts superclass reference back to subclass.

Animal a = new Dog();
If (a instanceof Dog) { Dog d = (Dog) a; } //Downcasting

7
New cards

What aspect of abstraction is also polymorphism?

Interfaces, because you are using the same method signature, but promotes loose coupling where components interact via interface, not class.

8
New cards