Introduction to OOP

Introduction to OOP

Introduction and need of OOP in development

Written by: saeed1907057

Sun, 17 Nov 2024

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. (Martin Golding)

Introduction to OOP

Before we go to OOP, lets understand a little about procedural programming.

Procedural programming

  • This is a way of writting code where a program is divided into methods each of them perform operation on data.
  • In this approach, program is divied into smaller parts called procedure/method.
  • This can be a good way for small applications.

Problems of Procedural Programming

  • Data and functions are separated. So it can be hard to maintain or change program.
  • If we have a feature that is implemented using multiple functions, they can't be bundled together. It reduces code readability.
  • It become harder to manage large program and dependencies among methods.
  • It may lead to code duplication.

What is OOP?

  • Object-oriented programming(OOP) is a way of writting program(programming paradigm) based on the concept of 'objects'. These 'objects' can contain data(as properties) and codes(as methods).
  • The core idea is to break the complex problem into smaller manageable objects.

How does OOP solve those problem?

  • Data and functions are bundled together. We don't need to worry about their dependencies.
  • Similar functions can be grouped together.
  • Relatively easy to maintain dependencies.
  • Code can be reused very much.

What else we get?

  • Codebase is easier to write and maintain and understand.
  • Can help to prevent unwanted code / bug.
  • Codebase can be divided based on features.
  • and many more.

Key concepts of OOP

Here we will understand the basic concepts. We will discuss them briefly later. This is just an overview.

  • Class

    • Anything we see can be considered as an class. For example: Chair, Table, Car, Human so on....
    • It hase some properties, like Human object has name, age properties.
    • It has some behaviours(methods), like Human can walk.
    • Example:
      class Human{
          private String name;
          private int age;
      
          public Human(String name, int age) {
              this.name = name;
              this.age = age;
          }
      }
      
  • Object

    • It is an instance of a class. Instance mean we assign some values to the properties of the class.
    • By assigning different values, we can create many many objects of a class.
    • Let's say, Human class have 2 properties name, age.
    • Now If we say there is a Human with name = Saeed, and age = 23. It becomes an object of the Human class since we have assigned some values to each properties.
    • Lets call the human sd, So sd = Human(name = Saeed, age=23).
    • Lets create another human named jn = Human(name = John, age = 45).
    • Both the sd and jn are object of the Human class.
    • Example:
      Human sd = new Human("Saeed", 23);
      Human jn = new Human("John", 45);
      
  • Encapsulation

    • It is the way of bundling(capsulate) data(attributes) and methods togethers, where method operates on the data.
    • It can hide implementation details of a class from outside and create public methods to interact with the class securely.
    • Ex: Here you can never set the age negative. (Will be discussed briefly later)
      class Human{
          private String name;
          private int age;
      
          public Human(String name, int age) {
              this.name = name;
              this.age = age;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public int getAge() {
              return age;
          }
      
          public void setAge(int age) {
              if(age < 0) {
                  return;
              }
              else{
                  this.age = age;
              }
          }
      }
      
  • Abstraction

    • It is the way of hiding complex implementation details while exposing only the necessary parts.
    • Classes and interfaces are used to do it.
    • For example: Vehicle class can have a method named move(), but each subclass(discussed later) like Car, Bike may implement the move() method differently.
    • But we don't need to worry about the implementation in subclasses, we can simply use the method of the Vehicle class and it will work.
    • For Example(not code): move() will call the correct method based on the object it is holding.
      Vehicle car = new Car();
      Vehicle bike = new Bike();
      
      car.move();
      bike.move();
      
    • Another example can be the Math.pow(double, double) function. we can simple use it and we know what it does. But we don't know how it does. Don't be confuse with hiding(discussed later).
  • Inheritance

    • It's a way to reuse code by inhering from another class.
    • It allows to create new class(subclass) by inheriting properties and methods of another class(parent).
    • For example: Dog class can be a subclass of Animal class. By using this Dog class can inherit properties like species and methods like eat(), walk() from the Animal class. Then we can add more properties or methods(like bark()) to the Dog class.
    • Example code:
      public class Bit2Byte {
      
          static class Animal{
              protected String species;
      
              public Animal(String species) {
                  this.species = species;
              }
      
              public void eat(){
                  System.out.println("Animal is eating");
              }
              public void walk(){
                  System.out.println("Animal is walking");
              }
          }
      
          static class Dog extends Animal{
              public Dog(String species) {
                  super(species);
              }
      
              public void bark(){
                  System.out.println(species+" Dog is barking");
              }
          }
      
          public static void main(String[] args) {
              Dog dog = new Dog("Canine");
              dog.eat(); // Animal is eating
              dog.bark(); // Canine Dog is barking
          }
      
      }
      
  • Polymorphism

    • It's like having many forms of a same things.
    • For example: int add(int, int) and int add(int,int,int) are different forms of same method add.
    • It makes the code more readable since we can add 2 or 3 values using the same function add, and appropriate method will be called.
    • Ex:
      public class Bit2Byte {
      
          private static int add(int a, int b) {
              return a + b;
          }
      
          private static int add(int a, int b, int c) {
              return a + b + c;
          }
      
          public static void main(String[] args) {
      
              int res = add(1,2);
              System.out.println(res); // 3
      
              res = add(1,2,3);
              System.out.println(res); // 6
          }
      
      }
      

That's the end. We will be discussing each topic in details in later sections.

There is no shortcut other than practicing.

User12024-11-02

This is a comment 1.

User12024-11-02

This is a comment 2.