OOP Flashcards

1
Q

What are the 4 pillars of OOP?

A

Abstractions, Polymorphism, Inheritance, 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 programming principle in which we centralize common characteristics and generalize Through abstraction, we hide underlying complexity through a simplified interface.
Think of a car - you do not need to know how the car works, just how to use the accelerator, brake, and steering wheel. Thus, a car “abstracts” away the internal details of the engine, motor, driveshaft, and other parts.
In Java, we achieve abstraction through abstract classes and interfaces.

Abstract classes are more general classes which cannot be instantiated. They instead act as templates for other classes to extend from. Abstract classes can have both concrete and abstract methods - the abstract methods must be implemented by concrete subclasses.
Interfaces also cannot be instantiated. They instead serve as contracts for methods that classes must implement. In order to inherit from interfaces, a class declares that it implements some interface, or multiple interfaces. Methods declared on an interface are implicitly public and abstract. Interfaces can have variables, but they are implicitly public, static, and final. Since Java 8, interfaces can also provide method implementations if the method is marked static or default.
Abstract classes are better suited for defining common characteristics of objects and are named as nouns by convention, whereas interfaces are better for defining common behavior the implementing class promises to provide.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Polymorphism

A

Polymorphism describes how objects can behave differently in different contexts. The most common examples of polymorphism in Java are method overloading and overriding.

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

What is Inheritance

A

In Java (and other OOP languages), classes contain a blueprint for the state and behavior of objects. Most languages have a method whereby classes (and thus, objects) can inherit the state and behavior (read: fields and methods) of other classes. The class from which other classes inherit from is called a “base” or “parent” class, and the class which inherit the parent is called a “child” or “sub”-class.

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

What is Encapsulation

A

Encapsulation is the OOP principle of containing related state and behavior together inside a class, and also hiding and preventing change to an object’s data members. When an object is encapsulated, it controls the access to its internal state. This prevents arbitrary external interference, which could bring the object into an invalid or inconsistent state.

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

What are the principles of SOLID

A

Single Responsibility - every class should have a single responsibility
Open-closed - open for extension, but closed for modification.
Liskov Substitution - replaceable with instances of their subtypes without altering the correctness of the program.
Interface Segragation - many client-specific interfaces are better than one general-purpose interface.
Dependency Inversion - Abstractions should not depend upon details;
Details should depend upon abstractions.
use the same level of abstraction at a given level.

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

What is the difference between an abstract class and an interface?

A

Conceptually, interfaces define behaviors and abstract classes are for concepts and inheritance.
You can implement multiple interfaces, but you can extend only one class.

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

Can abstract methods have concrete methods? Can concrete (non-abstract) classes have abstract methods?

A

Abstract classes can have concrete methods

Concrete classes cannot have abstract methods

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

Can static methods access instance variables? Can non-static methods access static variables?

A

No, static methods can access instance variables, ?

Yes, non-static methods can access static variables

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