OOP

11 Sep 2024 . java .

Object-oriented VS Process-oriented

  • Process-oriented divides the problem-solving process into methods and solves the problem through the execution of methods one by one.
  • Object-oriented abstracts objects and then solves the problem with the object execution methods, which is easier to maintain, reuse and extend.

Object Entity VS Object Reference

  • One entity can be referenced by many references, while one reference refers to 0 or 1 entity.
  • Object Equal compares the content, while Reference Equal compares the memory address.

Encapsulation

Hide the properties and implementation details of objects and expose public access methods to external to prevent arbitrary access and modification of data.

Inherit

Inherit the methods of the base class and make own extensions.

Polymorphism

One interface, multiple implementations.

Example:

class Shape {
    void draw() {}
}
  
class Circle extends Shape {
    void draw() {
        System.out.println("Circle.draw()");
    }
}
  
class Square extends Shape {
    void draw() {
        System.out.println("Square.draw()");
    }
}
  
class Triangle extends Shape {
    void draw() {
        System.out.println("Triangle.draw()");
    }
}