Java for Beginners – Functions & Object-Oriented Basics

 

1. What are Functions (Methods) in Java?

A function (in Java we call it a method) is a block of code that performs a specific task.
Instead of repeating code, you write it once inside a method and call it whenever you need it.

Example:

public static void greet() { System.out.println("Hello! Welcome to Java."); }

2. Why Use Methods?

✅ Reusability – Write once, use many times
✅ Cleaner code – Easier to read & maintain
✅ Better structure – Breaks big programs into smaller parts


3. Syntax of a Method

accessModifier returnType methodName(parameters) { // method body return value; // only if returnType is not void }

Example:

public static int add(int a, int b) { return a + b; }

4. Calling a Method

public class Example { public static void greet() { System.out.println("Hello!"); } public static void main(String[] args) { greet(); // method call } }

5. Methods with Parameters and Return Values

public class Calculator { public static int multiply(int x, int y) { return x * y; } public static void main(String[] args) { int result = multiply(4, 5); System.out.println("Result: " + result); } }

6. Method Overloading

Two or more methods can have the same name but different parameter lists.

public class MathOps { public static int add(int a, int b) { return a + b; } public static double add(double a, double b) { return a + b; } public static void main(String[] args) { System.out.println(add(5, 3)); // Calls int version System.out.println(add(2.5, 3.7)); // Calls double version } }

7. Introduction to Object-Oriented Programming (OOP)

Java is an object-oriented language, meaning everything revolves around classes and objects.

Key OOP Concepts:

  • Class → Blueprint or template for creating objects

  • Object → Instance of a class

  • Attributes → Variables inside a class (properties of the object)

  • Methods → Actions/behaviors inside a class


8. Creating a Class and Object

public class Car { String brand; int year; public void start() { System.out.println(brand + " is starting..."); } public static void main(String[] args) { Car myCar = new Car(); // create object myCar.brand = "Toyota"; myCar.year = 2022; System.out.println("Brand: " + myCar.brand); myCar.start(); } }

Output:

Brand: Toyota Toyota is starting...

9. Constructors in Java

A constructor is a special method that runs automatically when an object is created.

public class Student { String name; int age; // Constructor Student(String n, int a) { name = n; age = a; } public void display() { System.out.println(name + " - " + age); } public static void main(String[] args) { Student s1 = new Student("Poonam", 20); s1.display(); } }

10. Mini Practice Exercises

Task 1: Create a class Book with attributes title and author, and a method displayDetails(). Create objects for two books and display their details.

Task 2: Write a method isEven(int number) that returns true if the number is even, otherwise false.

Task 3: Create a Calculator class with overloaded methods for addition (int, double).


11. What’s Next?

In Blog 5, we’ll dive deeper into OOP concepts:

  • Access Modifiers

  • Inheritance

  • Method Overriding

  • Polymorphism

  • Encapsulation


💡 Tip: Think of a class like a "blueprint" and an object like the "real thing" built from that blueprint. Practice by modeling real-world things as Java classes.

Comments

Popular posts from this blog

Java for Beginners – Control Flow in Java Decisions & Loops

Understanding OOP -Inheritance, Polymorphism, Encapsulation

Getting Started with Java – Setup & Your First Program