OOP concepts Flashcards
What are the four main features of OOP
- Inheritance
- Encapsulation
- Polymorphism
- Data Abstraction
Polymorphism
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.
What are the two types of polymorphism in java
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
Difference between overloading and overriding a method
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.
Difference between primitives and objects in java
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.
Which collections have you used
Which interfaces have you used within collections
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
Difference between inheritance, polymorphism, composition
When and why would you use them
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.
What are annotations, and which annotations have you used?
spring specific?
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.
Difference between abstract class and interface and when would you use each?
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 would you write SQL on a table with employees and their region, which shows how many employees per each region?
SELECT region, COUNT(*) AS employee_count
FROM employees
GROUP BY region;
checked vs unchecked exception types
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.
builder pattern
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
observer pattern
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.
state and strategy pattern
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.