Java Abstract and Static Class Concepts
Java Abstract and Static Class Concepts
While abstract classes cannot be instantiated directly, they can have constructors to initialize fields common to all subclasses, offering a unified initialization strategy. This offers a structural advantage by ensuring every subclass receives base class constructor logic, helping maintain integrity and reducing redundancy in code where multiple subclasses share the same initialization pattern .
Abstract classes in Java serve to provide a template for other classes. They can contain implemented methods as well as abstract methods that must be implemented by subclasses. An abstract class cannot be instantiated directly using the 'new' operator, but they can have constructors which can be called upon object creation of subclasses. An abstract class can also be declared without containing any abstract method. If a subclass inherits an abstract class and does not provide implementations for all abstract methods, it should also be declared abstract .
In Java, static initialization blocks are executed when the class is loaded, whereas instance initialization blocks run when an instance of the class is created. The document example with `static { int x = 10; ... }` runs before the main method is executed due to its static nature. This leads to an understanding that the static initialization block outputs its print statement before `main` method statements. Hence, the output is '10 20' not '20 10', revealing execution priority of blocks over instance initialization .
Access modifiers in Java determine the visibility of class members. In the provided example, class A declares a public integer 'i' and a protected integer 'j'. Class B, which extends A, has access to 'i' directly because it is public. The protected member 'j' is accessible within B only through the super keyword, not directly when B redeclares a different integer 'j'. As a result, the output for the 'display()' method in class B shows '1 2', demonstrating isolated scopes for 'j'.
Inner class methods can access both their own instance variables and those of the enclosing class due to their contextual association within an instance of the outer class. This ability enables inner classes to functionally enhance the flexibility of the outer class, offering an integrated approach to handling operations that require cooperative access to both scopes, simplifying data manipulation without restrictive exposure .
Polymorphism allows methods to be invoked on objects of different classes at runtime. In the rate of interest example, abstract class `Bank` defines an abstract method `getRateOfInterest`, which is implemented differently by `SBI` and `PNB` classes. When a `Bank` reference points to `SBI` or `PNB`, `getRateOfInterest()` method calls SBI or PNB’s implementation. This decouples reference and method execution, showcasing polymorphism as the program dynamically resolves method during runtime .
The 'super' keyword in Java is used to call the constructor of the parent class from the child class. In the given code, class B extends class A, and within B's constructor, 'super()' calls the constructor of class A, initializing i and j to 1 and 2 respectively. This ensures that when the class B instance is created, it inherits and initializes parent class properties correctly. The correct output for the main class is '1 2', indicating that 'super()' is used correctly to inherit parent properties .
In Java, arrays are objects that contain a fixed number of values of a single type. The declaration `int a1[] = new int[10]` creates an array with space for 10 integers, initializing them to default values, here 0. Conversely, `int a2[] = {1, 2, 3, 4, 5}` initializes an array with specified elements directly. These allocations define the `length` property which is actively used to avoid out-of-bounds errors. The disparity between initialized lengths emphasizes the need for dynamic considerations based on use-case requirements to optimize memory and performance .
Static variables belong to the class, not to any specific instance, and as such, they maintain a single shared value across all instances. In the given example, every increment operation using the 'increment' method modifies the same `x` variable. First, `x` is set to 0 by obj1, then incremented twice, once by obj1 and once by obj2. Both obj1.x and obj2.x return the result '2', illustrating shared static state .
A member inner class in Java is a class defined within a body of an outer class. It can access all fields, methods, and the constructors of its containing outer class. They are associated with an instance of the outer class, meaning they need an instance of the outer class to be instantiated. This provides a logical grouping for classes and an ease of encapsulation, as well as access to outer class members .