BEFORE Programming 101 : OOP

BEFORE Programming 101 : OOP

Mastering Object-Oriented Programming: Unleashing the Power of Objectivity in Code Creation.

INTRODUCTION TO OOP:

Object-Oriented Programming (OOP) is a programming paradigm that provides a powerful and flexible way of organizing code, making it easier to create complex software systems that can be maintained and extended over time. OOP is widely used in various programming languages such as Java, C++, Python, and many others, and is an essential skill for any modern software developer.

CLASSES:

In Object-Oriented Programming (OOP), a class is a blueprint or template for creating objects. It defines a set of attributes (data) and methods (behaviors) that are shared by all instances (objects) of that class, and also provides a way to organize and encapsulate related data and behaviors, making it easier to create modular and reusable code.

\=> An example of a class in real life could be a class representing a bank account.

The bank account class might have attributes such as account number, account holder name, balance, and interest rate. It might also have methods such as depositing, withdrawing, and calculating interest.

Now Let's see some examples of classes writing in different programming languages :

\=> In Pyhton :

class Person:
    def __init__(self, name, age):
        # define an attributes :
        self.name = name
        self.age = age
    # define a method : 
    def say_hello(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person1 = Person("Alice", 25)
person1.say_hello() # Output: Hello, my name is Alice and I am 25 years old.

\=> In JavaScript :

class Person {
  constructor(name, age) {
    // Define an attributes :
    this.name = name;
    this.age = age;
  }
  // Define a method :
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

let person1 = new Person("Alice", 25);
person1.sayHello(); // Output: Hello, my name is Alice and I am 25 years old.

\=> In JAVA :

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        // define an attributes 
        this.name = name;
        this.age = age;
    }
    // Define a method :
    public void sayHello() {
        System.out.printf("Hello, my name is %s and I am %d years old.\n", name, age);
    }

    public static void main(String[] args) {
        Person person1 = new Person("Alice", 25);
        person1.sayHello(); // Output: Hello, my name is Alice and I am 25 years old.
    }
}

OBJECTS :

In Object-Oriented Programming (OOP), an object is an instance of a class. It represents a specific realization or manifestation of the attributes and behaviors defined by the class. However, it's created from a class by using a constructor method, which initializes the object's attributes with specific values. Once an object has been created, its methods can be called to perform various actions, and its attributes can be accessed to retrieve data.

\=> In our previous example of a bank account as a class, the objects would be the individual bank accounts themselves, and each bank account object would be an instance of the bank account class, with its own specific attributes such as account number, account holder name, balance, and interest rate.

From the previous Codes we will see each Object in the class :

\=> In Pyhton :

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def say_hello(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person1 = Person("Alice", 25)
person1.say_hello() # Output: Hello, my name is Alice and I am 25 years old.

In this Python code, the object is person1 which is an instance of the Person class that is created by calling the Person constructor with the arguments "Alice" and 25.

\=> In JavaScript :

class Person {
  constructor(name, age) {
    // Define an attributes :
    this.name = name;
    this.age = age;
  }
  // Define a method :
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

let person1 = new Person("Alice", 25);
person1.sayHello(); // Output: Hello, my name is Alice and I am 25 years old.

In the JavaScript code, the object is person1.

person1 is an instance of the Person class that is created by calling the Person constructor with the arguments "Alice" and 25. The constructor method of the Person class initializes the name and age attributes of the person1 object to "Alice" and 25, respectively.

\=> In JAVA :

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        // define an attributes 
        this.name = name;
        this.age = age;
    }
    // Define a method :
    public void sayHello() {
        System.out.printf("Hello, my name is %s and I am %d years old.\n", name, age);
    }

    public static void main(String[] args) {
        Person person1 = new Person("Alice", 25);
        person1.sayHello(); // Output: Hello, my name is Alice and I am 25 years old.
    }
}

In this Java code, the object is person1.

person1 is an instance of the Person class that is created by calling the Person constructor with the arguments "Alice" and 25. The Person constructor initializes the name and age attributes of the person1 object to "Alice" and 25, respectively.

Conclusion :

The reason that I give the example of those three programming languages is to show that OOP is not a PROGRAMMING LANGUAGE as I thought before and it's a way to write code in different programming languages.

In a Nutshell, by using OOP principles and techniques, we can write code that is more modular, flexible, and easier to understand and maintain, making it an essential skill for any programmer who wants to write high-quality, scalable, and maintainable code.

OOP is not limited to those two concepts and we will talk about others in the next chapter.

Stay Tuned :)