Basics of OOP Flashcards

1
Q

Object-oriented programming is

A

a paradigm based on the con-
cept of wrapping pieces of data, and behavior related to that
data, into special bundles called objects, which are construct-
ed from a set of “blueprints”, defined by a programmer, called
classes.

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

Data stored inside the object’s fields is often referenced
as ____, and all the object’s methods define its _____.

A

Data stored inside the object’s fields is often referenced
as state, and all the object’s methods define its behavior.

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

A parent class, is called a
____. Its children are ____.

A

A parent class, like the one we’ve just defined, is called a
superclass. Its children are subclasses.

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

Subclasses inherit ____
and ___ from their parent, defining only ___ or ____ that differ

A

Subclasses inherit state
and behavior from their parent, defining only attributes or
behaviors that differ

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

Assumingthatwehavearelatedbusinessrequirement,wecan
go even further and extract a more general class for all liv-
ing Organisms which will become a superclass for Animals
and Plants . Such a pyramid of classes is a

A

hierarchy. In such
a hierarchy, the Cat class inherits everything from both the
Animal and Organism classes.

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

What can subclasses override from parent classes?

A

Subclasses can override the behavior of methods they inherit from parent classes.

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

How can subclasses change the behavior of inherited methods?

A

Subclasses can either completely replace the default behavior or enhance it with extra features.

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

Can subclasses enhance the default behavior of inherited methods?

A

es, they can enhance the default behavior with some extra stuff.

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

Difference between Abstract Classes and Interfaces?

A

Abstract classes can have both abstract and concrete methods; whereas, interfaces can only have abstract methods.
An interface is a contract that specifies a set of methods that a class that is implementing must implement.

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

Can Association have many-to-many relationship?

A

In can be represented as one-to-one, one-to-many, or many-to-many (aka cardinality).

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

Code reuse

A

Using design patterns is one way to increase flexibility of software components and make them easier to reuse. However, this sometimes comes at the price of making the components
more complicated.

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

Encapsulate What Varies

A

Identify the aspects of your application that vary and separate them from what stays the same.

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

What should all products follow in the Factory Method pattern?

A

All products should follow the same interface, declaring methods that make sense for every product.

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

What should the return type of the factory method match?

A

The return type of the factory method should match the common product interface.

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

What should you replace product constructor references with in the creator’s code?

A

Replace them with calls to the factory method, extracting the product creation code into the factory method.

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

What might you need to add to the factory method to control the type of returned product?

A

A temporary parameter.

17
Q

What can you do if the factory method has a large switch operator?

A

you can create subclasses for each product type and override the factory method in them.

18
Q

What can you do if there are too many product types to create subclasses for all of them?

A

Reuse the control parameter from the base class in subclasses to determine the product type.

19
Q

What should you do if the base factory method becomes empty after extractions?

A

What should you do if the base factory method becomes empty after extractions?

20
Q

What is an alternative to creating new subclasses for multiple product types?

A

Pass an argument to the factory method to control which product type is returned.

21
Q

Q: What is a benefit of the Factory Method pattern regarding coupling?

A

A: It avoids tight coupling between the creator and the concrete products.

21
Q

Q: How does the Factory Method pattern support the Single Responsibility Principle?

A

A: It moves product creation code into one place, making the code easier to support.

22
Q

Q: How does the Factory Method pattern align with the Open/Closed Principle?

A

A: It allows introducing new product types without breaking existing client code.

23
Q

Q: What is a potential downside of using the Factory Method pattern?

A

A: The code may become more complicated due to the introduction of many new subclasses.

24
Q

Q: When is the Factory Method pattern easiest to implement?

A

A: When introducing it into an existing hierarchy of creator classes.

25
Q

Q: How does the Factory Method pattern relate to Abstract Factory?

A

A: Abstract Factory classes are often based on a set of Factory Methods.

26
Q

Q: How can Factory Method and Iterator work together?

A

A: They allow collection subclasses to return different types of iterators compatible with the collections.

27
Q

Q: How does Prototype differ from Factory Method?

A

A: Prototype isn’t based on inheritance and requires complicated initialization, while Factory Method is based on inheritance and doesn’t require initialization.

28
Q

Q: What is the relationship between Factory Method and Template Method?

A

A: Factory Method is a specialization of Template Method and may serve as a step in a larger Template Method.