Functions and Methods

Section 2: Functions and Methods

Lesson 1: Defining Functions and Methods

1.1 Creating Functions and Methods in Java

Functions (also called methods in Java) are blocks of code that perform a specific task. In Java, methods are defined within classes.

Example (Function in Java):

public class FunctionExample {

    // Function definition

    static void greet() {

        System.out.println("Hello, there!");

    }


    public static void main(String[] args) {

        // Function call

        greet();

    }

}


1.2 Parameters and Return Values

Functions can take parameters (input) and return values (output).

Example (Function with Parameters and Return Value):

public class MathOperations {

    // Function with parameters and return value

    static int add(int a, int b) {

        return a + b;

    }


    public static void main(String[] args) {

        // Function call with parameters

        int result = add(5, 3);

        System.out.println("Sum: " + result);

    }

}


Lesson 2: Object-Oriented Programming in Java

2.1 Classes and Objects

Java is an object-oriented programming (OOP) language. Objects are instances of classes, which encapsulate data and behavior.

Example (Class and Object):

// Class definition

class Car {

    String brand;

    int year;


    // Constructor

    Car(String brand, int year) {

        this.brand = brand;

        this.year = year;

    }


    // Method in the class

    void displayInfo() {

        System.out.println("Brand: " + brand + ", Year: " + year);

    }

}


public class ObjectExample {

    public static void main(String[] args) {

        // Creating an object of the Car class

        Car myCar = new Car("Toyota", 2022);


        // Accessing object's data and calling a method

        System.out.println("Brand: " + myCar.brand);

        myCar.displayInfo();

    }

}

2.2 Inheritance, Encapsulation, and Polymorphism

Inheritance: Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass).

Example (Inheritance):

// Superclass

class Animal {

    void eat() {

        System.out.println("Animal is eating.");

    }

}


// Subclass inheriting from Animal

class Dog extends Animal {

    void bark() {

        System.out.println("Dog is barking.");

    }

}


public class InheritanceExample {

    public static void main(String[] args) {

        // Creating an object of the Dog class

        Dog myDog = new Dog();


        // Accessing inherited method

        myDog.eat();


        // Accessing subclass method

        myDog.bark();

    }

}


Encapsulation: Encapsulation involves bundling data (attributes) and methods that operate on the data within a single unit, i.e., a class.

Example (Encapsulation): 

// Encapsulation with private data and public methods

class Person {

    private String name;


    // Getter and setter methods for name

    String getName() {

        return name;

    }


    void setName(String newName) {

        name = newName;

    }

}


public class EncapsulationExample {

    public static void main(String[] args) {

        // Creating an object of the Person class

        Person person = new Person();


        // Using getter and setter methods

        person.setName("John");

        System.out.println("Person's name: " + person.getName());

    }

}


Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass.

Example (Polymorphism): 

// Polymorphism with method overloading

class Calculator {

    int add(int a, int b) {

        return a + b;

    }


    double add(double a, double b) {

        return a + b;

    }

}


public class PolymorphismExample {

    public static void main(String[] args) {

        // Creating an object of the Calculator class

        Calculator myCalculator = new Calculator();


        // Method overloading

        System.out.println("Sum (int): " + myCalculator.add(5, 3));

        System.out.println("Sum (double): " + myCalculator.add(2.5, 3.7));

    }

}

With these concepts, you're equipped to create modular, reusable, and organized code using functions and embrace the principles of object-oriented programming in Java. Practice these examples and explore how they contribute to the flexibility and scalability of your Java programs.