Diff or When to go for abstract class and when for interface (1)
Diff or When to go for abstract class and when for interface (1)
JVM (Java Virtual Machine) is machine-dependent in the sense that each platform or
operating system requires its own implementation of the JVM. different hardware
architectures like x86, ARM, or others.
Encapsulation: Wrapping of the data (variables) and methods that into a single unit or
class, and restricting access to some of the object's components.
Abstraction: Hiding the complex implementation details and showing only the necessary
features of an object.
Interface: Defines a contract for what a class can do, Interfaces can only declare
methods (prior to Java 8) without implementations and constants.
Starting from Java 8, interfaces can also have default and static methods with
implementations.
Abstract Class: Provides a common base class for other classes to extend. An abstract
class can have both abstract methods (without implementation) and concrete methods
(with implementation). It can also have member variables and constructors. Or to maintain
a hierarchy
Inheritance: Creating a new class (derived class) that inherits attributes and behaviors
(methods) from an existing class (base class), As child parent relationship.
The "diamond problem" is a term often used in the context of multiple inheritance,
Polymorphism: When you override a default method, the overridden method in the class
will be called, even if the method is called on a reference of the interface type.
Compile time (method overloading), Run time (method overriding child-parent relation),
Explain the different access modifiers in Java.
Private: The members are accessible only within its own class.
Default: (No modifier) The member is accessible only within its own package.
Protected: The member is accessible within its own package and by subclasses in other
packages.
A class loader is a subsystem of JVM that loads Java classes dynamically during the
runtime. Every Java class is loaded by a class loader. When a Java program is executed,
class loaders are responsible for loading the class files into the JVM. It is a part of Java
Runtime Environment (JRE).
The JVM does not need to know about files and file systems, as this is delegated to the
class loader.
The bootstrap class loader is the first-class loader to load classes. It is responsible for
loading the core Java classes that are part of the JRE.
The extension class loader is responsible for loading classes from the JRE extension
directory.
The system class loader is responsible for loading classes from the classpath.
Class loaders are also used to implement security features. For example, the class loader
can be used to prevent classes from being loaded from untrusted sources.
Object class?
In Java, the Object class is the root class of all classes. Every class in Java implicitly or
explicitly extends the Object class. It resides in the java.lang package, which is
automatically imported into every Java program.
String literals that are interned are stored in this pool. When you create a string literal that
matches an existing one in the pool, the JVM returns a reference to the existing string
instead of creating a new one.
• You can explicitly intern strings using the intern () method, which ensures that
the string is stored in the intern pool.
• Interning strings can be useful for conserving memory and optimizing string
comparison operations.
Deserialization is the reverse process of reconstructing an object from its serialized form.
In Java, serialization is achieved by implementing the Serializable interface.
Serialization allows objects to be saved to files or sent over networks. Deserialization
(ObjectInputStream)reconstructs objects from byte streams, restoring their state.
Lambda expressions introduce a new syntax for defining anonymous functions in Java.
Lambda expressions are particularly useful when working with collections and the new
streams API.
Optional class is a container object that may or may not contain a value.It helps to
eliminate null
Exception example:
CompletableFuture.supplyAsync(() -> {
return "Result";
}).exceptionally(ex -> {
}).thenAccept(result -> {
});
// Lambda expression
static Keyword?
• The static keyword is used to declare static variables and static methods in Java.
• When a variable or method is declared as static, it belongs to the class rather than
to instances of the class. This means there is only one instance of the static
member shared across all instances of the class.
• Static variables are initialized when the class is loaded into memory and are
typically initialized with default values.
• Static methods can be invoked without creating an instance of the class.
• Static initialization blocks are executed when the class is loaded into memory, just
like static variables.
• They are particularly useful when static variables need complex initialization logic
beyond simple assignment.
• Static initialization blocks are executed in the order they appear in the class, from
top to bottom.
static {
// Static initialization block
staticVariable = calculateStaticVariable();
}
Comparator Interface:
• The Comparator interface is used to define custom comparison logic for sorting
objects.
• It contains a single method compare(Object obj1, Object obj2) that
compares two objects.
• Comparator objects can be passed to sorting methods to specify the custom
comparison logic.
public class MyComparator implements Comparator<MyClass> {
@Override
public int compare(MyClass obj1, MyClass obj2) {
return Integer.compare(obj1.getValue(), obj2.getValue());
}
}
Errors:
Errors are usually caused by the JVM or the underlying system encountering problems that
prevent it from continuing normal operation.
Errors are not meant to be caught or handled by applications. They indicate severe
problems that often require intervention at the system level or by the JVM itself.
Exceptions:
Using Thread.UncaughtExceptionHandler
thread.setUncaughtExceptionHandler((t, e) -> {
System.out.println("Exception in thread " + t.getName() + ": " + e.getMessage());
e.printStackTrace();
});
thread.start();
}
}
Using ExecutorService and Future: When using an ExecutorService to manage threads,
you can submit tasks that return a Future. You can then handle exceptions by calling the
get method on the Future, which will throw an ExecutionException if the task
encountered an exception.
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable:
• Does not return a result.
• Cannot throw checked exceptions.
• Functional method: void run().
• Used for simple tasks or when no result is needed.
Callable:
• Returns a result.
• Can throw checked exceptions.
• Functional method: V call() throws Exception.
• Used for tasks that need to return a result or handle exceptions. especially when
using ExecutorService for concurrent execution.
++Springboot and rest api url structure n basic bean life cycle
@annotations? restcontroller?webservice?
@autowired?
Sql basic? Joins? Union all? Why left outer join is faster?One sample sql havingclause?
Partition?
React js basic
Neo4j, graphQL, nodejs, java script, aws, gcp
Cloud, json/xml
Bi Tools custom
Design patterns?
1+2(3-2) expression resolution using visitor and tree, reverse link list? Sorting algorithms
with time complexity,
Java generics
Caching + kafka
Work@tech
Feedback——————————
introduction little slow abstraction vs interfae calarity asked only give ans
immutable, class custome, clonable (SHAWOD, DEEP) HOW? object copy exception
custome cutsome thread pool
++String pool
++design pattern
++class loaders
Neo4j, mongodb
Replit, redis
Render.com