Object-Oriented Fundamentals Flashcards

1
Q

What are the four core object-oriented programming concepts?

A

Abstraction, Encapsulation, Inheritance and Polymorphism

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

Explain Abstraction

A

Abstraction - means hiding low-level logic and exposing only the relevant details. Focusing on what the object does instead of how it does it. For example, you can drive a car not knowing how the engine works; You can log in using your username and password without knowing what calls are made to the backend.

Reducing the object to its essence. 
Abstraction defines an object in terms of its properties (attributes), behavior (methods) and interfaces (means of communicating with other objects.

Achieved using an abstract class or an interface.

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

Explain Encapsulation

A

Encapsulation - means binding the data (fields) and the code that manipulates them (methods) into a single unit and restricting external interference (example: creating a class with private properties but public getters/setters etc.). It can also be seen as a mechanism for limiting direct access to some of the object’s components (like exposing a getter but not a setter for a field). The goal is to prevent tight coupling (lower the dependency of one class on the other).

Uses access modifiers - public, private, protected.

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

Explain Inheritance

A
Inheritance -  is a reusability mechanism in OOP in which the common properties of various objects are exploited to form relationships with each other. Superclass (parent class) holds the logic and properties common for all subclasses (child classes), that inherit them when they extend the superclass. 
	Let's look at the Object class in Java as an example. All other classes are directly or indirectly derived from the class Object, and all of them have access to its methods (for example "Object::toString()").
An 'is-a' relationship exists between the subclass and superclass (Cat is and Animal).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain Polymorphism

A
Polymorphism - refers to "several forms" of an entity. In OOP, a method call can be interpreted in multiple ways, depending on the calling object, parameter object or object which contains the method. A simple example would be - both Dog and Cat are Animal and inherit the makeSound method from Animal, but for the Cat object, this method's output will be "meow", and for the Dog object it will be "bark". 
	There are two polymorphism types - static (at compile time) and dynamic (at runtime). 
	The abstraction mechanisms used in static polymorphism are method overloading (same method name, but different signature) and generics. 
	For dynamic polymorphism - method overriding (the subclass provides a specific implementation of a method, that exists in the superclass).
The ability of an object to take many "forms". Uses method overloading and overriding.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Methods signature

A

According to Oracle, the method signature is comprised of the name and parameter types. Therefore, all the other elements of the method’s declaration, such as modifiers, return type, parameter names, exception list, and body are not part of the signature.

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

When to use abstract classes vs interfaces?

A

Abstract Classes are a good fit if you want to provide implementation details to your children but don’t want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).

If you want to simply define a contract for Objects to follow, then use an Interface.
---
	An abstract class is a class that is declared abstract, and it may or may not have abstract methods, it can have static fields and static methods, it cannot be instantiated, but other classes can extend it. 
	Non-abstract subclasses must implement all of the parent class's abstract methods. 
An Interface is a declaration of methods of an object, it's not the implementation. The implementation is provided by the class that implements the interface. You can look at an interface as a contract between the class implementing the interface and the other classes that might use/interact with it. Since Java 8, interfaces have default methods. 

An abstract class can have final, non-final, static and non-static variables. It can also have private, protected, package-protected class members, but in a Java interface, all variables are public (can go against encapsulation) static and final by default. 

A class can extend only one abstract class, but can implement several interfaces.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between inheritance and composition?

A

Both Inheritance and composition are used to establish relationships between classes and objects. Inheritance derives one class from another, but composition defines a class as a sum of its parts.

Classes and objects created through Inheritance are tightly coupled (highly dependent on the others) because of the "is-a" relationship between the subclasses and the superclass (Car 'is-a' Vehicle). Changing the superclass can break your code. It makes sense to use inheritance if we know that the subclass will be a more specialized version of the superclass. 

Classes and objects created through composition are loosely coupled. The composition represents a "has-a" relationship, meaning, an object contains other objects as a part of its state (car has-a battery). Loose coupling provides more flexibility to the code.

If it's a HAS-A relationship, better to use composition.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the SOLID principles? What does each of the letters stand for?

A
Single Responsibility Principle (SRP)
Open/Closed Principle
Liskov's Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain Single Responsibility Principle (SRP)

A
every module or class should have responsibility for a single part of the software's functionality. A module or class should entirely encapsulate that responsibility. 
It also means keeping a class focused on a single concern - this will reduce the probability of the code in this class breaking because of a small change in another class/module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain Open/Closed Principle

A
Software entities should be open for extension, but closed for modification. 
	Open for extension: As requirements change, we can extend the module with new behaviors that satisfy the changes.
	Closed for modification: Extending the modules behavior does not result in changes to the source or binary of the module. In an extreme case, this means - never rewrite code, only write new code, but in most cases, this is unattainable.

“open for extension, but closed for modification.” classes’ properties can be inherited and used by subclasses etc, but not altered directly.

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

Explain Liskov’s Substitution Principle (LSP)

A

Objects of a supertype should be replaceable with objects of its subclasses without breaking the system.

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

Explain Interface Segregation Principle (ISP)

A

▪ No client should be forced to depend on methods it does not use.
▪ Many client-specific interfaces are better than one general-purpose interface.

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

Explain Dependency Inversion Principle (DIP)

A

High-level modules should not depend on low-level modules; Instead, both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. It’s called the DIP because, when this principle is followed, the dependency modules are reversed, rendering the high-level modules independent of the low-level implementation details.

In other words–don’t extend ArrayList class to get those functions; implement List interface instead.

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