Greeter Java Code Explanation and OOP Principles
Code Explanation
- Access Modifiers:
private: Only accessible within the same class- Example:
private final String name;means only methods inside the Greeter class can access this variable
- Example:
public: Accessible from any other class- Example:
public class Greetermeans other classes can use this class public void printGreetings()means other classes can call this method
- Example:
staticmodifier:- Belongs to the class itself, not to any specific instance
- Can be used without creating an object of the class
- Example:
private static final String DEFAULT_NAME- This variable is shared across all Greeter instances
- The same default name is available to all objects
public static void main(String[] args)- The main method is static because it needs to run without creating an object
finalmodifier:- For variables: Once assigned, value cannot be changed (like const in other languages)
- Example:
private final String name;- name cannot be modified after initialization
- Example:
- For static variables: Creates a constant
- Example:
private static final String DEFAULT_NAME- a constant default value
- Example:
- For variables: Once assigned, value cannot be changed (like const in other languages)
voidreturn type:- Indicates the method doesn’t return any value
- Example:
public void printGreetings()- prints to console but doesn’t return anything
The code demonstrates several OOP concepts:
- Encapsulation:
- Private fields (
name,campus) protect data from external access - Public methods provide controlled access to object functionality
- Private fields (
- Constructor:
public Greeter(String name, String campus) {
this.name = name;
this.campus = campus;
}
- Initializes new objects
- `this` keyword refers to the current instance
- Sets the initial state of object properties
- Instance Methods vs Static Methods:
- Instance method:
printGreetings()- operates on object data - Static method:
main()- runs independently of any object
- Instance method:
- Object Creation:
Greeter workshopGreeter = new Greeter(participantName, participantCampus);
- Creates a new instance of Greeter class
- Calls constructor with specific parameters
OOP Principles
The four main principles of Object-Oriented Programming (OOP) are:
- Encapsulation
- Bundles related data and methods that operate on that data within a single unit/class
- Controls access to internal details through access modifiers (private, public, protected)
- Example:
public class BankAccount {
private double balance; // Data is hidden
public void deposit(double amount) { // Public interface
if (amount > 0) {
balance += amount;
}
}
}
- Inheritance
- Allows a class to inherit properties and methods from another class
- Enables code reuse and establishes a relationship between parent and child classes
- Example:
public class Animal {
protected String name;
public void eat() { ... }
}
public class Dog extends Animal { // Dog inherits from Animal
public void bark() { ... }
}
- Polymorphism
- Ability of objects to take multiple forms
- Includes method overriding (runtime) and method overloading (compile-time)
- Example:
public class Shape {
public double getArea() { return 0; }
}
public class Circle extends Shape {
private double radius;
@Override
public double getArea() { // Method overriding
return Math.PI * radius * radius;
}
}
public class Calculator {
// Method overloading
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
}
- Abstraction
- Hides complex implementation details and shows only necessary features
- Achieved through abstract classes and interfaces
- Example:
public interface DatabaseConnection {
void connect();
void disconnect();
void executeQuery(String query);
}
public class MySQLConnection implements DatabaseConnection {
// Concrete implementation of abstract methods
public void connect() {
// Complex connection logic hidden from users
}
}
Benefits of following these principles:
- More maintainable and organized code
- Better code reusability
- Reduced complexity
- Easier debugging
- Better data security
- More flexible and extensible systems