Object Oriented Programming Flashcards

1
Q

What is the difference between a class and an object?

A

A class is a template for objects. A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an “instance” of a class.

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

What are the parts of a Java class?

A

Fields hold the data for an object. Fields are also called instance variables or object variables or properties.
Constructors don’t actually construct the object. The class makes the object and then executes a constructor to initialize the values of the fields.
Methods define what an object can do or the behavior of the object.

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

What is an access modifier?

A

There are four types of Java access modifiers:
Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

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

What is an interface?

A

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

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

What are the pillars of OOP?

A

The four pillars of object-oriented programming are Abstraction, Encapsulation, Inheritance, Polymorphism.

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

What is abstraction?

A

To abstract something away means to hide away the implementation details inside something – sometimes a prototype, sometimes a function. So when you call the function you don’t have to understand exactly what it is doing.

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

What is encapsulation?

A

The definition of encapsulation is “the action of enclosing something in or as if in a capsule”. Removing access to parts of your code and making things private is exactly what Encapsulation is all about (often times, people refer to it as data hiding).

Encapsulation means that each object in your code should control its own state.

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

What is inheritance?

A

Inheritance lets one object acquire the properties and methods of another object. In JavaScript this is done by Prototypal Inheritance.

Reusability is the main benefit here. We know sometimes that multiple places need to do the same thing, and they need to do everything the same except for one small part. This is a problem inheritance can solve.

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

What is polymorphism?

A

Polymorphism means “the condition of occurring in several different forms.” That’s exactly what the fourth and final pillar is concerned with – types in the same inheritance chains being able to do different things.

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

How does the Single Responsibility Principle relate to Java classes?

A

As the name suggests, this principle states that each class should have one responsibility, one single purpose. This means that a class will do only one job, which leads us to conclude it should have only one reason to change.

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

What is a package?

A

A package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for:
Preventing naming conflicts.
Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
Providing controlled access: protected and default have package level access control

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

How does an ArrayList<E> collection type compare to an array?</E>

A

An array is a fixed length data structure whereas ArrayList is a variable length Collection class.
We cannot change the size of an array once created but the size of ArrayList can be changed.
We cannot store primitives in an ArrayList. It can only store objects.

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

When is it appropriate to use a List collection type? And a Map collection type?

A

List is an ordered collection and can contain duplicate elements. You can access any element from its index. List is like an array with dynamic length. List is one of the most used Collection types. ArrayList and LinkedList are implementation classes of the List interface. The List interface provides useful methods to add an element at a specific index, remove/replace element based on the index and to get a sub-list using the index. The Collections class provides some useful algorithm for List – sort, shuffle, reverse, binarySearch etc.
Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap. The basic operations of Map are put, get, containsKey, containsValue, size, and isEmpty

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