OOP - Class 와 Object의 차이?

irob·2023년 8월 2일
0

Software Development

목록 보기
4/4

1. Class란?

  • A class is a template / blueprint / prototype from which objects are created.

2. Instance란?

  • An instance is a single and unique unit of a class.

3. Object란?

  • 객체(Object)는 현실의 대상(Object)과 비슷하여, 상태(state)나 행동(behavior) 등을 가지지만, 소프트웨어 관점에서는 그저 콘셉(concept), 즉 사유의 결과일 뿐이다.

정리하자면, 설계도-class-를 바탕으로 object를 소프트웨어에 실체화 하면 그것이 Instance가 되고, 이 과정을 인스턴스화(instantiation)라고 한다. 실체화된 instance는 메모리에 할당된다.

참고:
In OO Programming, we often hear of terms like “Class”, “Object” and “Instance”; but what actually is a Class / Object / Instance?

In short,
An object is a software bundle of related state and behavior.
A class is a blueprint or prototype from which objects are created.
An instance is a single and unique unit of a class.

Example, we have a blueprint (class) represents student (object) with fields like name, age, course, and methods like eat, sleep, study (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people).

Object:
Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and state (applying brake, changing gear).
An object stores its state in fields and exposes its behavior through methods.

Class:
Class is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.

Instance:
An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate a room of memory for that class instance.

++ 클래스의 속성값을 변화시키지 않고 Instance를 만드는 이유?
클래스안의 기능을 사용하기 위해 매번 같은클래스명과 같은 멤버를 반복적으로 호출하여 (돌려막기식으로)사용했어야 했던 것과는 달리,
클래스 복제본인 인스턴스를 여러 개 생성하고 각각의 인스턴스마다 (사용자가 필요한/혹은 넣고싶은) 다른 내용들을 넣어 사용할 수 있게 되었다.

profile
borison and me

0개의 댓글