Core Java Quick Reference Guide
Core Java Quick Reference Guide
Polymorphism in Java allows the same operation to behave differently on different data or classes. Compile-time polymorphism is achieved via method overloading, where methods have the same name but different parameters, like `show(int a)` and `show(String a)`. Run-time polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method already defined in its superclass, such as overriding `void show()` in `class B` that extends `class A`.
Overloading and overriding are both forms of polymorphism in Java. Overloading occurs when multiple methods in the same class have the same name but different parameters, illustrating compile-time polymorphism. Overriding happens when a subclass modifies the specific details of a method that its parent class defines, demonstrating run-time polymorphism. These allow Java to execute a method in different ways based on the object or parameters involved.
A constructor in Java is a special method used to initialize objects. It is called when a new object is created. There are different types of constructors: default (no parameters), parameterized (with parameters to pass specific initial values), and copy constructor (manual, to create an object by copying another object)
Control flow statements in Java dictate the order in which statements are executed, enabling complex program logic construction. The primary types include conditional structures such as 'if', 'if-else', and 'switch'; looping structures like 'for', 'while', and 'do-while'; and jump statements including 'break', 'continue', and 'return'. These constructs allow programmers to perform a series of operations conditionally, repeatedly, or based on events.
Primitive data types in Java, like int, float, and boolean, are predefined by the language and have a reserved size in memory, leading to efficient memory usage and fast performance. Non-primitive data types, such as Strings, Arrays, and Objects, are created by the programmer and can hold references to objects. These require more memory management and introduce overhead due to dynamic allocation and garbage collection, potentially affecting performance.
Java's platform independence is achieved through its ability to compile code into bytecode, which can run on any Java Virtual Machine (JVM) regardless of the underlying operating system. This eliminates the need for platform-specific code, making Java programs highly portable and versatile for deployment across various system environments.
Interfaces in Java allow the simulation of multiple inheritance by permitting a class to implement multiple interfaces, each potentially containing methods of the same signature or different ones. This overcomes the limitation of single inheritance (one superclass per class) by allowing classes to incorporate multiple sets of behaviors and functionalities, promoting flexible design patterns and abstraction layers. Interfaces are necessary because they enable separation of method declaration and implementation, enhancing code modularity and reuse.
Access modifiers in Java control the visibility of classes, methods, and variables, which is a key aspect of encapsulation. The visibility rules are as follows: 'private' restricts visibility to the same class; 'default' allows access within the same package; 'protected' permits access in subclasses and within the same package; 'public' allows access from any class. These rules help protect sensitive data and maintain encapsulation.
Java ensures security through features like the absence of pointers, which prevents unauthorized memory access, and its inherent security management via the JVM. This is crucial for developers as it helps prevent vulnerabilities and protects applications from exploits and malicious attacks.
Inheritance in Java is a mechanism that allows a class to inherit attributes and methods from another class, supporting a hierarchical structure of classes. It reduces redundancy and increases reusability by allowing shared attributes and behaviors to be defined in a base class and extended by subclasses. In Java, inheritance is limited to single inheritance with classes, but multiple inheritances can be achieved via interfaces.