OOP Intro Flashcards

1
Q

What is the difference between a class and an object?

A

A class is the blueprint 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
2
Q

What does instantiating mean?

A

“instantiating” is creating an object instance of a class.

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

What is the difference between stack and heap memory?

A

Stack is used for storing primitive types (numbers, boolean and character) and variables that store references to objects in the heap. Variables stored in the stack are immediately cleared when they go out of scope (eg when a method finishes execution). Objects stored in the heap get removed later on when they’re no longer references. This is done by Java’s garbage collector.

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

What are the problems of procedural code

A

Big classes with several unrelated methods focusing on different concerns and responsibilities.

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

What is encapsulation?

A

It suggests that we should bundle the data and operations on the data inside a single unit (class).

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

Why should we declare fields as private

A

We want to keep implementation details secret and prevent objects from entering bad state.

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

What is abstraction?

A

Suggests that we should reduce complexity by hiding the unnecessary implementation details. Encourages users to work with an interface.

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

What is coupling?

A

The level of dependency between software entities (eg classes). The more our classes are dependent on each other, the harder it is to change them.

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

How does the abstraction principle help reduce coupling?

A

By hiding the implementation details, we prevent other classes from getting affected when we change these details

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

What are constructors?

A

Called when we instantiate our class. We use them to initialize our objects.

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

What is method overloading?

A

Declaring a method with the same name but with different signatures. The number, type and order of its parameters will be different.

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

What are static methods?

A

These methods are accessible via classes, not objects.

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

How can we make one class inherit from another?

A

Through use of the extends keyword.

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

What does the hashCode() method of an object return?

A

It returns a numeric value that is calculated based on the address of the object in memory.

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

What is a default constructor?

A

A constructor without any parameters. If we don’t create it, the Java compiler will automatically add one to our classes.

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

How can we add a constructor to a class?

A

Create a method with no return type.

17
Q

Why is the super keyword used?

A

a reference to the base or parent class. We can use it to access the members (fields and methods) or call the constructors of the base class.

18
Q

What is the difference between private and protected access modifiers?

A

protected or private access modifiers are only accessible inside of a class. Protected members are inherited and are accessible by child (derived) classes. Private members are not.

19
Q

How accessible is a field or method if it’s declared without an access modifier?

A

If we omit the access modifier, the member will have the default access modifier which makes that member public in package. In other words, that member will be public in the package but private outside of the package.

20
Q

What’s the difference between method overriding and method overloading?

A

Method overriding means changing the implementation of an inherited method in a subclass. Method overloading means declaring a method with different signatures (different number, type and order of parameters).

21
Q

What is the benefit of using @override notation?

A

It signals the Java compiler that we’re overriding a method in the base class and this helps the compiler check our code for correctness. It will ensure the signature of the method in the subclass matches the on declared in the base class. Also, if we remove this method from the base class, the compiler will let us know and we can remove the method in the subclass as well.

22
Q

How is the instanceOf operator used?

A

It tells us if an object is an instance of a class. We use it before casting an object to a different type to make sure we don’t get a casting exception.

23
Q

What are the 4 principles of OOP?

A
  • Encapsulation: bundling the data and operations on the data inside a single unit (class).
  • Abstraction: reducing complexity by hiding unnecessary details (metaphor: the implementation detail of a remote control is hidden from us. We only work with its public interface.) -Inheritance: a mechanism for reusing code. -Polymorphism: a mechanism that allows an object to take many forms and behave differently. This will help us build extensible applications.
24
Q

When do we use abstract classes?

A

An abstract class is a partially-implemented (half-cooked) class. We cannot instantiate them. But we use them to share some common code across their subclasses.

25
Q

Can we have an abstract class without abstract methods?

A

Yes! An abstract class does not need abstract methods. But if we mark a method as abstract, we should mark the class as abstract as well.

26
Q

When do we use final classes?

A

Final classes cannot be inherited. We use them when we’ve made certain assumptions about a class and we want to prevent other classes extending our class and break those assumptions.

27
Q

What is the diamond problem?

A

The diamond problem happens in languages that support multiple inheritance. Java does not support multiple inheritiance.