Java-Methods
Q1. How can Java methods improve code reusability and maintenance ?
Java methods allow developers to extract repeated logic into a single reusable unit.
When the same functionality (such as calculating an average) is required in multiple places, it can be placed inside a dedicated method. This approach:
When the same functionality (such as calculating an average) is required in multiple places, it can be placed inside a dedicated method. This approach:
- Eliminates code duplication
- Improves readability
- Makes maintenance easier
Q2. When should a method be declared as static in Java ?
A method should be declared as static when it does not depend on instance variables or object state.
Static methods:
Static methods:
- Belong to the class, not to individual objects
- Can be called without creating an object
- Are ideal for utility or helper functions
Q3. What is the difference between method parameters and arguments in Java ?
In Java
Here:
When a method is invoked, arguments are assigned to parameters.
- Parameters are the variables defined in a method’s declaration.
- Arguments are the actual values passed to the method when it is called.
printDetails("Alice", 25);Here:
- "Alice" and 25 are arguments
- name and age in the method definition are parameters
When a method is invoked, arguments are assigned to parameters.
Q4. How does method overloading work in Java ?
Method overloading allows multiple methods to have the same name but different parameter lists within the same class.
Differences can be based on:
At compile time, Java determines which method to call based on the method signature and the arguments provided. This improves code clarity and flexibility.
Differences can be based on:
- Number of parameters
- Type of parameters
- Order of parameters
void log(String message) { }void log(int code) { }At compile time, Java determines which method to call based on the method signature and the arguments provided. This improves code clarity and flexibility.
Q5. How do abstract methods enforce behavior in Java inheritance ?
An abstract method is declared without an implementation and must be overridden by subclasses.
Example
Any class that extends Shape must provide its own implementation of the area() method. This ensures that all subclasses follow a common contract while allowing each to define its own logic.
Example
abstract class Shape { abstract double area();}Any class that extends Shape must provide its own implementation of the area() method. This ensures that all subclasses follow a common contract while allowing each to define its own logic.
Q6. Why must the main method be declared as public in Java ?
The main method must be declared as public so that the Java Virtual Machine (JVM) can access it from outside the class.
If the main method is not public:
This allows the JVM to invoke the method as the starting point of the application.
If the main method is not public:
- The program may compile successfully
- The JVM will fail to locate or access the entry point at runtime
- A runtime error will occur
public static void main(String[] args)This allows the JVM to invoke the method as the starting point of the application.