Summary of Core Java Interview Questions & Answers | Top FAQs Explained! | @Javatechie
Summary of "Core Java Interview Questions & Answers | Top FAQs Explained! | @Javatechie"
This video is a comprehensive tutorial aimed at preparing viewers for core Java interviews by explaining frequently asked questions, concepts, and practical coding examples. It covers fundamental and advanced Java topics, focusing heavily on Object-Oriented Programming (OOP) principles, Exception Handling, Java collections, and concurrency utilities.
Main Ideas, Concepts, and Lessons
1. OOP Pillars in Java
- Encapsulation:
- Keep fields private, expose them via public getter/setter methods.
- Example: Entity or DTO classes with private attributes and public accessors.
- Inheritance:
- Reuse properties from parent classes/interfaces.
- Use
extends
for class-to-class or interface-to-interface inheritance,implements
for class-to-interface. - Example: Service interface and its implementation.
- Polymorphism:
- Achieved via method overloading (same method name, different parameters) and overriding (child class redefines parent method).
- Runtime polymorphism demonstrated by injecting interface rather than implementation.
- Overriding rules: method signatures must be the same; return types can be covariant (child return type).
- Static and private methods cannot be overridden (static methods are hidden).
- Abstraction:
- Hiding implementation details, exposing only functionality.
2. Exception Handling
- Exception Hierarchy:
- Throwable → Error / Exception → Checked / Unchecked (Runtime) Exceptions.
- Checked exceptions require handling (compile-time warning), unchecked do not.
- Overriding and Exceptions:
- If parent method throws an exception, child method may or may not throw it.
- If child throws an exception, parent must declare it.
- Final, Finally, and Finalize:
final
: keyword to prevent reassignment or inheritance.finally
: block always executes for cleanup.finalize()
: method called by JVM’s garbage collector before object destruction.
- Custom Exceptions:
- Extend
Exception
(checked) orRuntimeException
(unchecked). - Provide constructors passing messages to super class.
- Use
throw
to raise exceptions,throws
to declare them in method signatures.
- Extend
- Try-Catch-Finally with Return:
- Finally block executes even if try or catch returns.
- Can be bypassed by
System.exit()
or throwing exceptions in finally.
3. Java Interfaces (Java 7 vs Java 8)
- Java 7 interfaces can only have abstract methods.
- Java 8 introduced
default
methods (with implementation) andstatic
methods inside interfaces. - Default methods provide backward compatibility without forcing implementation overrides.
- Static methods in interfaces serve common utility functions.
4. String Handling
- String Creation:
- Using
new String()
creates objects in heap and string literal pool (SCP). - Using string literals uses SCP only; duplicates are avoided via SCP reuse.
intern()
method retrieves SCP reference.
- Using
- Immutability of String:
- Strings cannot be modified after creation; concatenation creates new objects.
- Immutability ensures security (e.g., password safety), thread-safety, and efficient hashing.
- Mutable Alternatives:
StringBuffer
(thread-safe, synchronized).StringBuilder
(not synchronized, faster).- Used for dynamic string manipulation.
- Creating Custom Immutable Classes:
- Declare class
final
. - Make all fields
private final
. - No setters, only getters.
- Initialize fields via constructor.
- Return defensive copies or clones of mutable fields in getters.
- Declare class
5. Password Storage
- Prefer
char[]
overString
for passwords because: - Strings are immutable and remain in memory until GC.
char[]
can be overwritten after use, reducing exposure risk.
6. Marker Interfaces
- Interfaces with no methods, used to signal metadata (e.g.,
Serializable
,Cloneable
). - You can create custom marker interfaces for tagging classes.
7. Java Collections Framework
- Collection Interfaces: List, Set, Map.
- Common Implementations:
- List:
ArrayList
,LinkedList
. - Set:
HashSet
,LinkedHashSet
,TreeSet
. - Map:
HashMap
,LinkedHashMap
,TreeMap
.
- List:
- Differences:
- List allows duplicates and indexed access.
Notable Quotes
— 00:00 — « No notable quotes »
Category
Educational