2. Object-Oriented Questions Flashcards
What is polymorphism? Give an example.
It’s when classes have different functionality while sharing the same interface.
For example, you can have an Employee
class, a Contractor
class that inherits from Employee
, and a PermanentEmployee
class that also inherits from Employee
. If the Employee
class has a method called CalculateSalary
, the Contractor
and the PermanentEmployee
classes could have two entirely different implementations of it. Meaning that even though they share the same interface, the behaviour of the objects changes.
Method overloads are also a form of polymorphism.
What is encapsulation and abstraction?
Encapsulation prevents access to implementation details whereas abstraction allows making relevant information visible. In fact, abstractions are usually implemented as abstract classes or interfaces.
What is inheritance?
It’s when a class is based on another class, taking on its characteristics.
Can a class inherit multiple classes?
No. However, it can implement multiple interfaces.
What are the four pillars of OOP?
- Abstraction
- Polymorphism
- Inheritance
- Encapsulation