얼마전 스터디에서 Object의 정의에 대한 열띤 토론이 있었습니다. 추상적인 개념에 대한 토론이라 어떤 결론을 내릴 수가 없었습니다. 전 "기존에 사용되는 의미"에 집중해서 Object가 무엇인지 살펴보도록 하겠습니다.

Object에 대한 정의는 Java Tutorial에 한페이지 정도의 분량으로 설명이 돼있습니다. Object는 Object-Oriented 기술의 핵심이며 실제 세상의 Object는 주변에서 둘러 볼 수 있는 모든 것이라는 말이 첫문장에 나옵니다.

실제 세상의 Object는 크게 두가지 특성이 있는데 바로 상태행동이다. 실제 세상이 객체가 가지는 상태와 행동을 식별해내는 것이 Object-Oriented Programing을 시작하는 가장 중요하다. "이 객체는 무슨 상태를 가질 수 있을까?" "이 객체는 무슨 행동을 할까?" 등을 질문해보면 실제 세상의 Object가 얼마나 복잡한가 알 수 있을 것이다. 그리고 어떤 Object는 다른 Object를 가지고 있을 수도 있을 것이다. 이러한 관찰을 모두 Object-Oriented Programming 세상으로 옮기면 된다.

A circle with an inner circle filled with items, surrounded by gray wedges representing methods that allow access to the inner circle.

위에 보이는 그림은 실제 세상이 아닌 소프트웨어에서의 Object를 표현한 것이다. 소프트웨어에서의 Object는 실제 세상의 Object와 매우 비슷하다. 이들(소프트웨어의 Object) 또한 상태와 행동을 가지고 있다. 이 Object는 상태를 Field에 담아 두고 행동을 Method로 표현한다. 여기서 Field는 프로그래밍에서 변수에 해당하며 Method는 몇몇 프로그래밍 언어에서 Function으로 표현되기도 한다. 메소드는 객체의 내부 상태에 작용을 가하며 객체와 객체간 의사소통의 기반이 된다. 객체의 상태를 숨기고 모든 작용은 객체의 메소드를 통해서 가능하도록 하는 것은 data encapsulation(은닉화)로 알려져 있으며 객체 지향의 기본 원리에 해당한다.

객체를 사용하여 코딩할 때의 장점들은 다음과 같다.

  • Modularity : The source code for an object can be written and maintainedindependently of the source code for other objects. Once created, anobject can be easily passed around inside the system.
  • Information-hiding: By interacting only with an object's methods, thedetails of its internal implementation remain hidden from the outsideworld.
  • Code re-use: If an object already exists (perhaps written by anothersoftware developer),you can use that object in your program. This allows specialists toimplement/test/debug complex, task-specific objects, which you can thentrust to run in your own code.
  • Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

마지막 부분은 원문이 더 보기가 좋을 것 같아서 그냥 긁어 왔습니다.(사실은 귀차니즘?ㅋ) Java는 객체 지향 언어이며(완벽하진 않은 것 같습니다만..잘 몰라서 이렇게 밖에 말을 못하겠네요.) Java Tutorial에 위와 같이 정의를 해두고 있습니다. 이건 마치 법이나 마찬가지라고 생각합니다. Java의 근본인 Object에 대한 개념을 나름대로의 개념으로 재정의 하겠다는 것은 위험한시도라고 생각합니다. :)