OOP Flashcards

1
Q

What are the pillars of OOP?

A

1) Encapsulation - is distilling a concept down to a discrete set of data and behaviors.
2) Inheritance - is a form of code reuse. When a type extends another type, it incorporates all non-private super-class fields and methods as members.
3) Polymorphism - When one or more subclasses override a super-class method or implement an interface, their behavior is said to by polymorphic. All types have the same method signature, but the outcome differs between types.
4) Abstraction - is the principle of revealing only the essential characteristics of a type.

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

What is the relationship between a class and object?

A

In Java, the definitions for object state and behavior are stored in a class. A class is a contract. It specifies the data that makes up each object instance. It defines the behaviors allowed.

In other words, a class is a template for an object. An object is an instance of a class.

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

Access modifiers (public, protected, private) support which pillar?

A

Encapsulation

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

What is inheritance? How does it work?

A

Inheritance is a code reuse strategy. Through inheritance, a child class gains access to a parent class’s fields and methods. It inherits a parent’s data and behavior. That means the code to create the data and behavior is only stored once, in the parent. When a child extends a parent, it reuses the parent’s code.

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

Describe a syntax or keyword that supports another pillar.

A

A class encapsulates code that solves a problem.

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

I’ve heard the phrase, “prefer composition over inheritance.” What does it mean? Is it right?

A

With composition, it’s easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type.

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

How do the access modifiers (public, protected, private) interact with inheritance?

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

How do I call a parent class’s constructor from a child class?

A

Through inheritcance.

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

Tell me about the keyword: this. What does it mean? What does it do?

A

The this keyword accesses class members. A member is any field or method declared inside the class code block.

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

What is the maximum number of classes you can extend (inherit)?

A

In Java, we can only extend a single class.

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

What is the dot operator?

A

The (.) operator is also known as member operator it is used to access the member of a package or a class.

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

Can one class both extend a parent and implement an interface?

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

What is the maximum number of interfaces you can implement?

A

As many as needed.

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

What is state? How does OOP manage it?

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

What is a constructor?

A

A constructor is a method-like subroutine that ensures an object is created in a valid state. It ensures an object has the data it needs to operate.

Constructors can call other constructors by using the this keyword: this(argument, anotherAgrument);

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

Explain the difference between the 3 access modifiers.

A

public - All classes can access the member.

protected - Only classes in the same package or a subclass can access the member.

private - The member is only visible from inside the class containing the definition. No outside classes may access it.

17
Q

What is the keyword for inheritance?

A

Extends

18
Q

What is an interface?

A

An interface enforces a contract for classes that implement it. It does not contain implementation. An interface specifies behavior without the need to create an explicit method.

19
Q

What is object instantiation?

A

It is the process of providing the data needed, if any, to code that constructs an object.

The new operator creates an object.

20
Q

Java, the definitions for object state and behavior are stored in what?

A

A class

21
Q

What is a class? Provide an example.

A

A class is a contract/blueprint. It specifies the data that makes up each object instance. It defines the behaviors allowed.

Ex: A blueprint is instructions for building a house, but it isn’t a house. It’s just specifications for a house. When a house is built using the blueprint, it’s an object produced from that blueprint.

22
Q

What data type is a class?

A

A class is a composite data type. A composite data type is a type made of other data types. For example, we could create a Pet data type by combining two strings: one for the pet’s name and one for its kind (dog, cat, ferret, etc).

The Pet class serves as a template for individual pets. It defines what’s possible. From the class, we can create instances of a pet.

23
Q

What is the difference between static and non-static methods?

A

Non-static methods have access to fields. Static methods don’t. One way to think about it is that static methods are just independent methods with no context.

Non-static methods can’t be executed independently. They’re part of an object. They have access to an object’s state.

24
Q

Static vs. Non-Static: What type of methods are executed using an instance of an object?

A

Non-static

25
Q

Static vs. Non-Static: What type of methods are executed using a class name?

A

Static methods

26
Q

What is polymorphism?

A

When two classes have identical method signatures but different behaviors, they exhibit polymorphism.

In plain English, it’s when an object of one type executes a method and an object of a different type executes the same method (or at least the same signature) and the behavior is different.

27
Q

What is composition?

A

Composition is a way to model relationships by including complex types as fields in other types.

28
Q

What is the keyword for interfaces?

A

Implements

29
Q

True or False?: Interfaces are Java data types.

A

True

30
Q

When should you use an interface?

A

Rule #1: Use an interface when there are two or more concrete implementations that solve one problem. For example, Visa, Discover, and American Express resolve payments in completely different ways. Their implementations are different. But their payment resolution contract is consistent (or should be). We can create a PaymentProvider interface that establishes the contract.

Rule #2: Use an interface when one concrete implementation solves a problem but there’s a possibility of different implementations. For example, we might create a game that’s initially limited to human players. If there’s a possibility of an AI player in the future, add a Player interface to establish the player contract, implement the interface in HumanPlayer, and leave the door open for other concrete Player implementations in the future.

Rule #3: Use an interface to control testing.