Core Java Interview Questions Guide
Core Java Interview Questions Guide
Java provides four access specifiers: public, protected, private, and default (no modifier). A public class, method, or variable is accessible from anywhere. Protected members are accessible within their own package and by subclasses. Private elements are accessible only within their own class, ensuring encapsulation. The default grants access only to other classes in the same package, limiting visibility outside. These specifiers control how classes interact and help enforce encapsulation by protecting internal states and behaviors .
The Java Virtual Machine (JVM) is an abstract machine that enables computers to run Java programs. It provides a runtime environment that converts Java bytecode into machine code so that it can be executed on the host machine. The JVM is part of the Java Runtime Environment (JRE), which provides the libraries, Java command, and other components to run applications. The Java Development Kit (JDK) includes the JRE and additional tools necessary for Java development, such as compilers and debuggers. Therefore, while the JRE allows for running Java applications, the JDK is necessary for developing them. The JVM is the execution engine that runs the compiled bytecode .
Abstract classes in Java can contain both abstract methods (without a body) and methods with implementations, whereas interfaces can only declare abstract methods until Java 8, which introduced default and static methods. Abstract classes can have fields with any visibility, undefined in interfaces. Use abstract classes when there is a strong inheritance relationship, sharing code among closely related classes, whereas interfaces are ideal for defining a contract that can be implemented by many unrelated classes. Interfaces facilitate multiple inheritances of type and behavior, making them suitable for sharing method signatures across different categories of classes .
Method overriding in Java occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. This enables runtime polymorphism because the JVM determines the method to be executed at runtime based on the object's actual type. This allows for different behaviors in subclasses that share a common interface. Method overloading, on the other hand, is compile-time polymorphism where multiple methods can have the same name but differ in parameter types or numbers. Overloading does not involve inheritance and is resolved at compile time thus providing different ways to perform a similar operation within the same class .
Static methods and variables belong to the class itself rather than any instance of the class. Static variables are shared among all instances of a class, making them useful for defining constants or shared class values. Static methods cannot access instance variables or methods directly unless they are passed an object reference explicitly. Instead, they are utilized for operations not dependent on instance data or for tasks like utility functions. In contrast, instance variables and methods are tied to specific objects, managing individual object states .
Encapsulation is a fundamental principle of object-oriented programming in Java that involves bundling the data (fields) and the methods that manipulate the data into a single unit, a class. By controlling access to the fields and requiring all interaction to occur through method calls, encapsulation preserves the integrity of the data and prevents outside interference and misuse. This allows for more secure and flexible code as fields can be modified without affecting outside code, and any modifications to the internal implementation can be done without changing the interface .
The `this` keyword in Java is used within an instance method or constructor to refer to the current object. It is commonly used to resolve name conflicts between instance variables and parameters or local variables, and to pass the current object as a parameter to another method or constructor. Additionally, it can be used for constructor chaining within the same class to invoke another constructor, providing a clear structure in object initialization .
Packages in Java provide a namespace that helps in organizing classes and interfaces into a folder structure, improving organization and readability in large applications. They help avoid naming conflicts by differentiating classes with the same name within different packages. Packages also help in controlling access with protected and default scope, facilitating encapsulation. .
Inheritance in Java is a mechanism where one class (subclass) inherits fields and methods from another class (superclass). It promotes code reusability, allowing for the implementation of new functionality in subclasses without altering existing system code. Inheritance supports polymorphism, enabling objects to be treated as instances of their superclass, and provides a natural hierarchy among classes. This reduces redundancy and enhances maintainability in object-oriented programming .
Constructors in Java are special methods that are called when an instance of a class is created, primarily used to initialize the object's state. They have the same name as the class and no return type, unlike regular methods. While constructors can have parameters, they can only be called at object creation. Regular methods can be called multiple times and perform operations or computations, not limited to initialization tasks. Constructors contribute to setting up required states for objects before use .