OOP Flashcards

1
Q

What are the 4 pillars of OOP? Explain and give examples of how you’d implement them

A

Inheritance, Encapsulation, Abstraction, Polymorphism

Inheritance - A mechinism by which one class is allowed to inherit the fields and methods of another class. By extending a class from another, you create a parent child relationship which allows the child class to access the fields and methods of the parent

Encapsulation - Encapsulation is defined as the wrapping up of data under a single unit which protects the code from being accessed outside of the unit. Setting the fields of a class to private and having getters and setters be the only way to access the code is a form of encapsulation

Abstraction - The process which makes it so that only the essential details are displayed to the user. Implementation of abstract classes and and interfaces are ways of reaching 100% abstraction.

Polymorphism - The ability to have similarly named methods perform different functions, either through compile time polymorphism in method overloading (giving a method multiple uses based on the parameters), or runtime polymorphism in method overriding (changing the method instructions in child classes)

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

Explain the principles of the SOLID acronym

A

S - Single Responsibility Principle: Each class should be responsible for one functionality in a program
O - Open Closed Principle: Software components should be open for extension, but not for modification
L - Liskov Substitution Principle: Objects of a superclass should be replaceable with objects from the subclass w/o breaking
I - Interface Segregation Principle: No client should be forced to depend on methods it does not use
D - Dependency Inversion Principle: High level modules should not depend on low level modules, both should depend on abstraction

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

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

A

Abstract classes can have concrete and abstract methods, but interfaces can only have abstract methods

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

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

A

Abstract classes can have both concrete & abstract methods, however, non-abstract classes cannot have abstract methods.

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

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

A

A static method cannot access instance variables as static methods cannot use the “this” keyword to reference an object
Non static methods however can reference static methods directly

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

What are the implicit modifiers for interface variables/methods?

A

Interface methods are by default, abstract and public, whereas interface variables are by default public, static, and final

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

What is the difference between method overloading and overriding? What are the rules for changing the method signature of overloaded methods?

A

Overloading - Compile-time polymorphism that allows methods with the same name to have different functions depending on the parameters

Overriding - Runtime polymorphism that allows child class methods to change the function of its similarly named parent method

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

Can you overload/override a main/static/private/default/protected method?

A

Static - You can overload static methods, but cannot override it as there can be no runtime polymorphism
Main - The main method is static, so it cannot be overridden, It can however be overloaded, but the JVM will never call it
Private - Yes you can overload a private method, but cannot access it from the same class. It cannot be overridden
Default - it cannot be overridden
Protected - it can be overridden

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

What are covariant return types? What rules apply to return types for overridden methods?

A

A return type that can vary when the method is overridden. The return type must be a subtype of the parent’s return type

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

When do you use extends or implements keywords?

A
You use extends when you want to inherit the properties from another class
You use implements when you want to implement an interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly