Java 2
Java 2
The static keyword in Java is a non-access modifier primarily used for memory management.
It applies to variables, methods, blocks, and nested classes. It signifies that a particular member belongs to
Static Variables:
- These variables are initialized only once and are stored in a fixed memory location.
Static Methods:
Static Block:
2. Enhances code organization by associating methods with the class rather than objects.
2. Define inheritance. What are the benefits of inheritance? Explain the various forms of inheritance.
Inheritance is a mechanism where one class (child class) acquires the properties and behaviors of another
class (parent class). It facilitates code reuse and establishes a hierarchical relationship between classes.
Benefits of Inheritance:
1. Code Reusability: Allows child classes to reuse the code of parent classes.
Forms of Inheritance:
1. Single Inheritance: One child class inherits from one parent class.
Polymorphism refers to the ability of a method or object to take multiple forms. It can be achieved through
- Occurs when multiple methods in the same class have the same name but different parameters.
- Occurs when a subclass provides its own implementation of a method defined in the parent class.
1. The method in the child class must have the same name, return type, and parameters as the parent class
method.
2. The method cannot have a more restrictive access level than the overridden method.
Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime
rather than compile-time. It is implemented using a parent class reference to refer to a child class object.
Advantages:
5. With suitable code segments, illustrate various uses of the final keyword.
The final keyword is used to restrict the modification of variables, methods, or classes.
Final Variables:
Final Methods:
Final Classes:
- Cannot be subclassed.
Advantages:
The abstract keyword defines classes and methods that cannot be directly instantiated.
Advantages:
1. Promotes abstraction.
Advantages:
Packages are a way to group related classes and interfaces. They prevent naming conflicts and simplify code
organization.
Importing Packages:
Features:
Advantages:
1. Promotes reusability.
Methods: