Module 8 – Introduction to OOP

වස්තු-පාදක ක්‍රමලේඛනය (Classes & Objects)

1. OOP යනු කුමක්ද?

Object-Oriented Programming (OOP) යනු සැබෑ ලෝකයේ ඇති "වස්තු" (objects) සංකල්පය මත පදනම් වූ ක්‍රමලේඛන ක්‍රමවේදයකි. මෙහිදී, අපි දත්ත (attributes) සහ එම දත්ත මත ක්‍රියා කරන ශ්‍රිත (methods) එකට එකතු කර "වස්තු" නිර්මාණය කරමු.


2. Class සහ Object

  • Class: Class යනු වස්තුවක් නිර්මාණය කිරීම සඳහා වන සැලසුමයි (blueprint). උදාහරණයක් ලෙස, 'Car' යනු class එකකි. එහි attributes (වර්ණය, මාදිලිය) සහ methods (start(), stop()) අඩංගු විය හැක.
  • Object: Object යනු class එකකින් සාදන ලද අවස්ථාවකි (instance). උදාහරණයක් ලෙස, 'myRedToyota' යනු 'Car' class එකෙන් සෑදූ object එකකි.

3. Java Class එකක් නිර්මාණය කිරීම

Java හි class එකක් නිර්මාණය කිරීමට class යන keyword එක භාවිතා කරයි.

public class MyClass {
    // Attributes (fields) and methods go here
}

ප්‍රායෝගික උදාහරණ 10ක්

1. සරල `Car` Class එකක් සෑදීම
// We define a class named Car
public class Car {
    // An attribute of the Car class
    String color = "red";
}
මෙය සැලසුම පමණි. මෙය run කළ විට කිසිදු ප්‍රතිඵලයක් නොලැබේ.
2. Class එකකින් Object එකක් සෑදීම

`Car` class එකෙන් `myCar` නමින් object එකක් සාදා එහි attribute එකක් පෙන්වමු.

public class Example2 {
    public static void main(String[] args) {
        Car myCar = new Car(); // Create an object of the Car class
        System.out.println("The car color is: " + myCar.color);
    }
}
// Assume the Car class from Example 1 is in a file named Car.java
ප්‍රතිඵලය:
The car color is: red
3. Attributes කිහිපයක් සහිත Class එකක්
public class Student {
    String firstName = "Nimal";
    String lastName = "Silva";
    int age = 15;
}

// In another file:
public class Example3 {
    public static void main(String[] args) {
        Student myStudent = new Student();
        System.out.println("Name: " + myStudent.firstName + " " + myStudent.lastName);
    }
}
ප්‍රතිඵලය:
Name: Nimal Silva
4. Object එකක Attribute අගයක් වෙනස් කිරීම
public class Example4 {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.color = "blue"; // Change the attribute value
        System.out.println("The new car color is: " + myCar.color);
    }
}
ප්‍රතිඵලය:
The new car color is: blue
5. Class එකකට Method එකක් එකතු කිරීම
public class Dog {
    // Method inside the Dog class
    public void bark() {
        System.out.println("Woof! Woof!");
    }
}

// In another file:
public class Example5 {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.bark(); // Calling the method
    }
}
ප්‍රතිඵලය:
Woof! Woof!
6. Constructor එකක් භාවිතය

Constructor යනු object එකක් සාදන විට ස්වයංක්‍රීයව ක්‍රියාත්මක වන විශේෂ method එකකි. එය class එකේ නමට සමාන වේ.

public class Person {
    String name;

    // Constructor
    public Person(String personName) {
        name = personName; // Set the initial value for the name attribute
    }

    public static void main(String[] args) {
        Person myPerson = new Person("Kamal");
        System.out.println("Person's name is: " + myPerson.name);
    }
}
ප්‍රතිඵලය:
Person's name is: Kamal
7. Constructor එකක් සහ Method එකක් සහිත Class එකක්
public class Circle {
    double radius;

    public Circle(double r) {
        radius = r;
    }

    public double getArea() {
        return 3.14 * radius * radius;
    }

    public static void main(String[] args) {
        Circle myCircle = new Circle(5.0);
        System.out.println("Area of the circle is: " + myCircle.getArea());
    }
}
ප්‍රතිඵලය:
Area of the circle is: 78.5
8. Objects කිහිපයක් සෑදීම

එකම class එකෙන් විවිධ attributes සහිත objects කිහිපයක් සෑදිය හැක.

public class Example8 {
    public static void main(String[] args) {
        // Assume Person class from example 6 exists
        Person person1 = new Person("Amal");
        Person person2 = new Person("Bimal");

        System.out.println(person1.name);
        System.out.println(person2.name);
    }
}
ප්‍රතිඵලය:
Amal
Bimal
9. `this` Keyword එකේ භාවිතය

Constructor එකේ parameter නම සහ attribute නම සමාන වන විට, ඒවා වෙන් කර හඳුනා ගැනීමට `this` භාවිතා කරයි.

public class Employee {
    String name;

    public Employee(String name) {
        // this.name refers to the class attribute
        // name refers to the parameter
        this.name = name;
    }
}
10. සරල Bank Account Class එකක්
public class BankAccount {
    double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

    public void deposit(double amount) {
        this.balance += amount;
    }

    public double getBalance() {
        return this.balance;
    }

    public static void main(String[] args) {
        BankAccount myAcc = new BankAccount(10000.0);
        myAcc.deposit(5000.0);
        System.out.println("Current balance is: " + myAcc.getBalance());
    }
}
ප්‍රතිඵලය:
Current balance is: 15000.0