OOP Flashcards
What are main 3-4 OOP principles?
1) Abstraction;
2) Inheritance;
3) Polymorphism;
4) Encapsulation;
What is abstraction?
Abstraction is a process that allows us to model reality by removing details that are not important for our representation.
Applying abstraction means that each object should only expose a high-level mechanism for using it.
This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.
Also, we apply abstraction when we select those which are the attributes which an object is made of.
What is encapsulation?
Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods.
What is inheritance?
In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
What is polymorphism?
Polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types
There are 3 forms of polymorphism:
- ad hoc polymorphism, known also as overloading;
- parametric polymorphism;
- subtyping.
What is Overloading?
A name is overloaded when it corresponds to more than one object and context information must be used to determine which object is denoted by a specific instance of that name.
The most common examples of this are:
- the use of the name “+” to indicate either integer or real addition and sometimes also concatenation of strings;
- the ability to define more than one function (or constructor) with the same name but whose instances differ in the number or type of their parameters.
What is Parametric Polymorphism?
A value shows parametric polymorphism when it has an infinite number of types which can be obtained by instantiating a single schema of general type.
Why do we need encapsulation?
Encapsulation is more about reducing dependency between modules, so improvement can be made “quietly” with little or no expense to other modules interacting with it, than it is of security. Because the interacting modules depend on the “strict external interface or strict contract”.
If i don’t provide my client with a setter and gave them direct access to a variable, and i realize that i need to set some restriction on the variable before my client could use it, me changing it, could be me, changing the life of my client, or application of my client with HUGE EXPENSE. But if i provided the “strict contract” by creating a “strict external interface” i.e setter, then i can easily change my inner workings with very little or no expense to my clients.
What is composition?
Object composition is a way to combine objects or data types into more complex ones.
What are the benefits of composition over inheritance?
It is more natural to build business-domain classes out of various components than trying to find commonality between them and creating a family tree. Composition also provides a more stable business domain in the long term as it is less prone to the quirks of the family members. It also allows to avoid breaking Liskov's substitution principle. We avoid breaking the symmetry rule of the equal because given a instance of a derived class and b an instance of a base class: a.equals(b) would return true and b.equals(a) would return false. Since a instance of b would return true and b instance of a would return false.
The order of object initialization in Java
- all static blocks and fields of a parent class
the same happens in a child class - all non-static blocks and fields of a parent class
invocation of a parent constructor - initialization of non-static fields and blocks in a child class
invocation of a child constructor