OOP Flashcards

1
Q

What are main 3-4 OOP principles?

A

1) Abstraction;
2) Inheritance;
3) Polymorphism;
4) Encapsulation;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is abstraction?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is encapsulation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is inheritance?

A
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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is polymorphism?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Overloading?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Parametric Polymorphism?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why do we need encapsulation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is composition?

A

Object composition is a way to combine objects or data types into more complex ones.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the benefits of composition over inheritance?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The order of object initialization in Java

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly