Object Oriented Programming Flashcards
What is the difference between a class and an object?
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.
What are the parts of a Java class?
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.
What is an access modifier?
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.
What is an interface?
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.
What are the pillars of OOP?
The four pillars of object-oriented programming are Abstraction, Encapsulation, Inheritance, Polymorphism.
What is abstraction?
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.
What is encapsulation?
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.
What is inheritance?
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.
What is polymorphism?
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 does the Single Responsibility Principle relate to Java classes?
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.
What is a package?
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 does an ArrayList<E> collection type compare to an array?</E>
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.
When is it appropriate to use a List collection type? And a Map collection type?
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