OOP Flashcards

1
Q

Fundamental principles of object-oriented programming.

A

Fundamental principled of object-oriented programming:
** data encapsulation: Hiding internal state and requiring all interaction to be performed through an object’s methods

Bundling code into individual software objects provides a number of benefits, including:

Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
Information-hiding: By interacting only with an object’s methods, the details of its internal implementation remain hidden from the outside world.
Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

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

Polymorphism

A

Benefits:
* it makes the code more dynamic - it allows the actual decision of which method is to be invoked to be taken at runtime based on the actual class of object(dynamic binding)
* it makes the code more reusable

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

Inheritance

A

*Multiple inheritance of implementation
- the ability to inherit instance methods from multiple classes.Default methods introduce one form of multiple inheritance of implementation. A class can implement more than one interface, which can contain default methods that have the same name. However, such a class cannot be compiled. In this case, the implementing class is required to provide its own implementation of the common method to avoid ambiguity.

*Multiple inheritance of type
- includes ability to implement multiple interfaces and/or ability to extend from multiple classes

*(NOT SUpported in JAVA)Multiple inheritance of state
-includes ability to inherit instance fields from multiple classes

Interfaces, classes, and enums are all “types”. Java allows a class to implement multiple interfaces. In this way, Java supports multiple inheritance of types.
“State”, on the other hand, is represented by instance fields. Only a class can have instance fields and therefore, only a class can have a state. (Fields defined in an interface are always implicitly static, even if you don’t specify the keyword static explicitly. Therefore, an interface does not have any state.) Since a class is allowed to extend only from one class at the most, it can inherit only one state. Thus, Java does not support multiple inheritance of state.
https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html

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