Java Core Flashcards
Java core concepts
What is a lambda expression?
It provides a way of implementing functional interfaces in a more readable way.
What are the four pillars of OOP? Explain each one of them.
Encapsulation (data hiding)
Abstraction (implementation hiding) - focus on what it is done rather than how it does, Reduces code complexity
Inheritance (reusability) - inheriting properties and behaviors from its parent.
Polymorphism (multiple forms) - an object can play more than one role.
What is the difference of method overloading and overriding?
Both are in the context of polymorphism.
Overloading (compile time) - methods have same name but different signatures.
Overriding (runtime) - same method in parent and child, but the implementation used will be checked/decided when the code is run.
Explain to me what are the equals() and hashCode() methods and how they are related.
They are both methods that are inherited from Object class.
equals() should be used for object comparison, once, the parent’s method compares the reference of the objects (so if I want another type of comparison, I need to override it).
hashcode() returns a single unique integer (hash value) that will be used in a hash based collection.
Why should I override it? If I am using a hash collection with the default hashcode() the collection will not sense duplicated objects inside of it.
What is the equals(), hashcode() contract?
If equals returns true, hashcode must return the same value.
If hashcode is different, then equals must return false.
If two objects, have the same hashcode, they may or may not be equal (hash collisions can happen)
What is an idempotent endpoint?
It is an endpoint that produces the same result no matter how many times it is called, as long as the request is identical.
Which HTTP methods requests are idempotent?
Yes: GET, PUT, and DELETE (either deleted or already gone).
No: POST (always create a new resource), and PATCH (multiple calls may cause unintended changes).
What is the difference between REST and RESTful?
REST is an architectural style, with 6 (5 mandatory) constraints. An API is RESTful only if it adheres to all the mandatory constraints.
What are the mandatory constraints in REST architeture?
- Client-server app
- Stateless
- Cacheability (it tells client to store that response for x time).
- Uniform Interface (use standards: HTTP methods, HTTP codes).
- Layered System (client-load balancer-server-database)
- Code-on-Demand (optional): server can return executable code.
What are the main differences between an interface and an abstract class?
Interface: all methods are abstract (except for the default methods); no variables (only if it is final static), and the main purpose is behavior specification (contract).
Abstract class: can have both abstract and concrete methods, can have properties, and it is used for code reuse and partial implementation.