OOP concepts Flashcards

1
Q

What are the four main features of OOP

A
  1. Inheritance
  2. Encapsulation
  3. Polymorphism
  4. Data Abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Polymorphism

A

Means to have many forms or perform an action in many different ways. (Poly means many and Morph means to change)

In Java this means we can have a single interface with many different implementations.

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

What are the two types of polymorphism in java

A

Compile time or Run time polymorphism

Compile time is static polymorphism - achieved by function overloading.

Function overloading is when you have multiple functions with the same name but different parameters. You can overload by changing the number of args or type of args,

Timetime polymorphism - dynamic - a function call to an overridden method is resolved at runtime. METHOD OVERRIDING.

DOUBLE CHECK ALL THIS

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

⁠Difference between overloading and overriding a method

A

Method overloading is compile time; you can have multiple methods of the same name and give them different params. The return types can be different so it is the same method name but different signature. You MUST change the parameters for this to happen.

Method overriding is run time polymorphism. A method in a derived class has the same name, return type and parameters but the derived class provides a specific implementation.

When to use either? Overriding when you want to extend or modify the behavior of the method in an inherited subclass and overloading is when you want the same behaviour but handle different input types or parameters.

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

⁠Difference between primitives and objects in java

A

Data Types: Primitive types are pre defined while objects are user defined. Primitives e.g. int (32bit), long(64bit), double (64bit), char (16bit)

Memory usage: Primitives more efficient as they consume less memory and retrieve values quicker. E.g int uses 4 bytes of memory and Integer uses more.

Representation: primitives represent a value and tend to not have methods. While Objects have methods.

Primitive types hold the actual values, direct in memory, while reference data types hold references or memory addresses of objects. E.g. they point to objects stored in the memory heap.

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

Which collections have you used

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

Which interfaces have you used within collections

A

The java collections framework provides different types of collections. The core interfaces include -

List, Set, Queue and Map.

List - ordered collections and duplicates

Set - store unique elements - maintains uniqueness through a hashmap

Queue - manages elements in a FIFO order

Map - key value pairs. Another form of java set - cannot contain duplicate elements.

Concurrent Hashmap

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

Difference between inheritance, polymorphism, composition

When and why would you use them

A

Inheritence - Inheritance is a concept of OOPs where one class inherits from another class that can reuse the methods and fields of the parent class - promotes code reuse and the “IS - A “ relationship between classes.

Syntax – class child extends parent. onsider a group of vehicles. You need to create classes for Bus, Car, and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be the same for all three classes.

extends keyword is used for inheriting one class into another
Inheritence - classes share a similar behaviour.

Polymorphism - structural mechanism allowing same operation to behave differently based on the context used e.g. arguments passed in and return values.

Composition - used to promote a “HAS A “ relationship. In java achieved by using instance variables of other objects

e.g. a person who has a Job - class with a job field. Basically create objects which contain other objects.

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

What are annotations, and which annotations have you used?
spring specific?

A

Meta data that provide additional information about the code but do not change the programs logic. Used to give instructions to the compiler, generate code and influence behaviour of frameworks and tools.

Apply to classes, methods, fields and parameters

@Override - when a method is supposed to override a method in parent class. Will help catch errors at compile time if it doesn’t override anything.

@Test - marks method as a test to be run by JUnit. Recognise as a test by testing framework.

@Autowired - used for dependency injection in spring. Simplifies wiring of beans, reducing boiler plate code and readability of dependent injection.

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

Difference between abstract class and interface and when would you use each?

A

Abstract class is a class that cannot be instantiated directly, serves as a blueprint for other classes to derive (extend) from. Can have implemented and abstract methods (can have member variables - final, non final, static or non static)

Interface specifies a set of methods that a class must implement. Methods are abstract.Variables are implicitly public, static and final (constants)

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

How would you write SQL on a table with employees and their region, which shows how many employees per each region?

A

SELECT region, COUNT(*) AS employee_count
FROM employees
GROUP BY region;

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

checked vs unchecked exception types

A

Checked - exceptions checked at compile time. So Java compiler enforces you to handle these exceptions in a TRY CATCH or declaring an exception with THROWS keyword in method signature. Use for expected scenarios you can recover from.

Derived from Exception class but not RuntimeException class.

Represent conditions that a reasonable application might want to catch e.g. file not found, network issue

IOException: input/output issue such as reading a file that doesn’t exist.

SQLException: thrown when issue accessing data base.

Unchecked - exceptions not checked at compile time. Java compiler does not enforce handling or declaring these is method signature, typically result from programming errors and can be avoided by careful coding.

Derived from RuntimeException usually represent logic errors.

Examples - NullPointerException (application trying to use null in a case where object is expected) ; ArrayIndexOutOfBoundsException (trying to access item in array with invalid index) ; ArithmaticException e.g. division by 0.

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

builder pattern

A

A creational design pattern that provides a flexible solution for creating complex objects. Allows the construction of an object to be separated from its representation. It enables you to make different representations of the same object.

Allows constructing objects step by step instead of all at once. e.g. create an object with many possible configurations. Using METHOD CHAINING

benefits - readable code, immutability (state cannot change after creation); flexibility - easier to add optional params without modifying existing constructors

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

observer pattern

A

behavioural - Loose object communications - when you are interested in the state of an object but want to get notified when there is any change. So you have the object being watched called the subject and the object watching called the observer.

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

state and strategy pattern

A

state pattern used to manage state of an object and its behaviour based on state. Object changes behaviour when internal state changes. Strategy defines a family of algorithms , encapsulating each one and making them interchangeable. Client selects algo at runtime.

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

mongodb vs postgres vs sql

A

MONGO DB - schema less, stored JSON. So something with varying data formats could be good here. Flexible schema.

POSTGRES - relational db, schema based, close to SQL standards. relational organises data into tables / relations of rows and columns. Has advanced features.

SQL - relational db - schema based.

NOSQL -
schema less? key value store, e.g mongo db.

17
Q

Primitives vs Reference types

A

primitives access data direct, in memory. Reference access data by referencing data or pointing to something in the heap (where the data is stored in memory) e.g. objects and arrays.

18
Q

Describe the Different Primitive Types and the Amount of Memory They Occupy

A

byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2^31 to 2^31-1
long 8 bytes -2^63 to 2^63-1
float 4 bytes ±3.40282347E+38F (6-7 significant)
double 8 bytes ±1.79769313486231570E+308 (15-16 sig.)
char 2 bytes 0 to 65,535
boolean Varies Typically 1 byte or more due to padding

19
Q

What Are the Restrictions on the Members (Fields and Methods) of an Interface Type?

A

FIELDS: Implicitly public, static and final

public - all public access from any class.

static - fields belong to interface itself rather than instances of the interface

final - fields constants cannot change after initialisation.

METHODS: public and abstract

abstract methods have no body and must be implemented by class implementing interface

Default Methods: Interfaces can have methods with a body using the default keyword. These methods provide a default implementation and are not required to be overridden by implementing classes.

Static Methods: Interfaces can also have static methods. These methods belong to the interface itself and are called using the interface name, not through instances of the interface.

20
Q

final, private, protected, static - explain

A

final - for constants and preventing modifications. for fields; for methods it prevents them being overridden; classes prevents class being subclassed, cannot be extended.

private - fields cannot be directly accessed outside fo the class; methods, cannot be accessed outside of class; also use inner classes to restrict visibility to enclosing classes

Protected - allows fields, methods and inner classes access from same package from subclasses.

STATIC -class level members belong to class itself rather than instance. probably utils class here example - can access without creating an instance of that class.

21
Q

Describe the Difference Between equals() and ==

A

In Java, == is an operator that compares the references or memory addresses of objects to determine if they are the same, whereas . equals() is a method that compares the contents of the objects to check for value equality.

22
Q

What Is an Anonymous Class? Describe Its Use Case

A

Inner classes with no name. So we cannot make instances of them, we instantiate them in a single class expression at the point of use.

Usually when something is temporary or there is a one off situation required. Is it like a plastic cutlery of Java?

e.g. thread creation – providing an implementation of the runnable interface.