3. Polymorphism Flashcards
What does polymorphism mean?
Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.
What is one of the ways that we can use Polymorphism?
Creating interfaces is one of the ways we can use polymorphism.
What is an interface?
An interface is a contract that allows us to know that particular public methods and properties will always be available on a class that implements the interface.
What’s the default and the only access modifier an interface property or method can have?
Public. They cannot be set to anything else.
How do you add business logic to interfaces?
You do not. You only add the API calls themselves.
How does a class implement an interface?
Exactly the same way that it extends a base class to it. By using the interface’s name after columns next to the class’s own name. Though, it must also implement the properties and methods defined in the interface.
eg. class ConcreteClass : BaseClass, IMyInterface { ... }
How do you test for whether an object’s class implements a certain interface?
By casting the object as the interface using the as
keyword. This will return null
if the class doesn’t implement the interface and the same object if it does.
eg. object as IMyInterface; // return true if it does, false if not.
What is composition?
Composition is extracting common business logic between classes into a separate class and reuse that class.
What does composition allow us to do?
Composition allows us to share the same code in multiple places.
What is the sealed
keyword?
It is a modifier that prevents a given class from being inherited by others.