0% found this document useful (0 votes)
2 views4 pages

Method

The document provides an overview of methods in Java, explaining their definition, advantages, and types including predefined, user-defined, overloaded, and overridden methods. It also covers special methods such as static, final, abstract, and recursive methods, along with best practices for writing effective methods. Overall, it emphasizes the importance of methods for code reusability, readability, and modular programming.

Uploaded by

Philip Balagtey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Method

The document provides an overview of methods in Java, explaining their definition, advantages, and types including predefined, user-defined, overloaded, and overridden methods. It also covers special methods such as static, final, abstract, and recursive methods, along with best practices for writing effective methods. Overall, it emphasizes the importance of methods for code reusability, readability, and modular programming.

Uploaded by

Philip Balagtey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

I.

Introduction to Methods
A. What is a Method?

 A method in Java is a block of code that performs a specific task.


 Methods allow code reuse, improve readability, and help with modular programming.
 Java methods are similar to functions in other programming languages.

B. Advantages of Using Methods

1. Code Reusability – Write once, use multiple times.


2. Modularity – Break a large program into smaller, manageable parts.
3. Readability – Improves code clarity and organization.
4. Debugging – Easier to troubleshoot and fix errors.

II. Defining and Calling Methods in Java


A. Syntax of a Method

A method in Java consists of:

 Access Modifier (public, private, protected, or default)


 Return Type (void or a specific data type)
 Method Name (should be meaningful and follow camelCase)
 Parameter List (optional)
 Method Body (enclosed within {})

Example of a Simple Method

java
CopyEdit
public class Example {
// Method definition
public static void greet() {
[Link]("Hello, welcome to Java!");
}

public static void main(String[] args) {


// Method call
greet();
}
}

B. Calling a Method

 Methods are called by their name followed by parentheses ().


 If the method is static, it can be called directly in main().
 If the method is non-static, it requires an instance of the class.

Example of Calling a Non-Static Method

public class Example {


// Non-static method
public void displayMessage() {
[Link]("This is a non-static method.");
}

public static void main(String[] args) {


Example obj = new Example(); // Creating an object
[Link](); // Calling the method
}
}

III. Types of Methods in Java


A. Predefined Methods (Built-in Methods)

Java provides many built-in methods in standard libraries like Math, String, and Arrays.

Examples of Built-in Methods

java
CopyEdit
public class BuiltInMethods {
public static void main(String[] args) {
[Link]([Link](16)); // Square root
[Link]([Link](2, 3)); // 2 raised to the power 3
[Link]("Hello".length()); // String length
}
}

B. User-Defined Methods

Developers can define custom methods.

Example: Method with Parameters and Return Type

java
CopyEdit
public class Calculator {
// Method with parameters and return type
public int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


Calculator calc = new Calculator();
int result = [Link](5, 10);
[Link]("Sum: " + result);
}
}

C. Method Overloading

 Defining multiple methods with the same name but different parameters.

Example of Method Overloading

java
CopyEdit
public class OverloadingExample {
// Overloaded methods
public void print(int num) {
[Link]("Integer: " + num);
}

public void print(String text) {


[Link]("String: " + text);
}

public static void main(String[] args) {


OverloadingExample obj = new OverloadingExample();
[Link](10);
[Link]("Java");
}
}

D. Method Overriding (In Inheritance)

 A subclass provides a specific implementation of a method inherited from a parent class.

Example of Method Overriding

java
CopyEdit
class Parent {
public void showMessage() {
[Link]("Message from Parent class");
}
}

class Child extends Parent {


@Override
public void showMessage() {
[Link]("Message from Child class");
}
}

public class OverridingExample {


public static void main(String[] args) {
Child obj = new Child();
[Link](); // Calls the overridden method in Child class
}
}

IV. Special Methods in Java


A. Static Methods

 Belong to the class, not an instance.


 Called using the class name.

Example of a Static Method

java
CopyEdit
public class StaticExample {
public static void display() {
[Link]("This is a static method.");
}

public static void main(String[] args) {


[Link](); // Calling without creating an object
}
}

B. Final Methods

 Cannot be overridden in subclasses.

Example of a Final Method

class Parent {
public final void display() {
[Link]("This is a final method.");
}
}

class Child extends Parent {


// Cannot override final method
}

C. Abstract Methods

 Declared in an abstract class, must be implemented by subclasses.

Example of an Abstract Method

java
CopyEdit
abstract class Animal {
abstract void makeSound(); // Abstract method
}
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Woof! Woof!");
}
}

public class AbstractExample {


public static void main(String[] args) {
Dog myDog = new Dog();
[Link]();
}
}

D. Recursive Methods

 A method that calls itself.

Example of a Recursive Method (Factorial Calculation)

java
CopyEdit
public class RecursionExample {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


[Link]("Factorial of 5: " + factorial(5));
}
}

V. Best Practices for Writing Methods in Java


1. Use meaningful method names (e.g., calculateArea() instead of calc()).
2. Follow camelCase naming convention for method names.
3. Keep methods short and focused (one method should do one thing well).
4. Use parameters and return values appropriately instead of relying on global variables.
5. Document methods using comments or Javadoc (/** ... */).
6. Avoid too many arguments – consider using objects to group related parameters.
7. Follow method visibility principles (use private unless exposure is necessary).

VI. Conclusion
 Methods are fundamental to writing efficient and reusable Java code.
 Java supports multiple types of methods, including static, final, abstract, and recursive methods.
 Method overloading and overriding allow flexibility in defining behavior.
 Proper method design and best practices improve code readability and maintainability.

You might also like