OOP Concepts Flashcards

1
Q

What are the 4 pillars of OOP? Explain each and give examples of you implement them in Java code

A

Abstraction - the process of showing only essential features of an entity/object to the outside world and hiding the irrelevant information.

Encapsulation - wrapping up data and member function together into a single unit. You can use encapsulation to provide security by limiting what pieces of code can be accessed.

Inheritance - enables new objects to take on the properties of existing objects. There is single inheritance, mulit-level inheritance, and hierarchical inheritance. You can use inheritance to create a series of classes that inherit from one another for example a dog class that inherits from an animal class.

Polymorphism - the ability to redefine methods for derived classes.An example of this would be creating a child and a parent class and having the child inherit from the parent and then overriding some of the parents methods.

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

SOLID stands for:
S - Single-responsiblity Principle.
O - Open-closed Principle.
L - Liskov Substitution Principle.
I - Interface Segregation Principle.
D - Dependency Inversion Principle

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 constants, members, method stubs (method without a body) and defined methods.

An interface can only have constants and method stubs.

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

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

A

A concrete class cannot contain abstract methods. Abstract methods can have concrete 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

Static methods cannot access instance variables.

Non-static methods can access static variables.

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

The implicit modifiers for interface variables are public, static, and final.

The implicit modifiers for interface methods are public.

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

Method Overloading is a compile time polymorphism. In method overloading, return type doesn’t have to be the same, but you must change the parameters.

Method overriding is a run time polymorphism. In method overriding the derived class provides the specific implementation of the method that is already provided by the base class or parent class. Return type must be the same. Method overloading may have different return types, different access modifiers, throw different checked or unchecked exceptions.

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

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

A

No you can’t override a main method but, you can overload a main method.

No you can’t override a static method but you can overload a static method.

No you can’t override a private method, but you can overload a private method.

No you can’t override a default method, but you can overload a default method.

Yes you can override a protected method, and you can overload a protected method.

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

Covariant return type refers to return type of an overriding method. It allows to narrow down return type of an overridden method without any need to cast the type or check the return type. Covariant return type works only for non-primitive return types.

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

Extends is used when you want attributes of a parent class or interface, extends does not have to use all of the methods in a super class.Implements is used when you want attributes of an interface in your class, implements has to use all of the methods of the interface.

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

What are enumerations (enums)?

A

Enumerations represent a group of constants.

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

What are the implicit modifiers for interface variables / methods?

A

Interface variables are implicitly public, static, and final. Interface methods are implicitly public.

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