• Rather than asking “how do you implement this function”, you ask “who should hold this function?”
  • 좋은 객체지향 설계는 역할 분담과 협력 구조가 명확하다.

Class

What is it?

Class

A class is a blueprint or template for creating objects

  • defines the structure and behavior (attributes and methods) that the objects created from it will have
  • like a recipe
  • Members
    • Fields
    • Methods
class Person 
{
    String name;     // 필드 (상태)
    void greet() {   // 메서드 (행동)
        System.out.println(name + "님, 반가워요!");
    }
}
 
class 클래스명 
{
    // 필드 (속성)
    // 메서드 (행위)
    // 생성자
    // 이너 클래스 (선택)
}
  • For getter and setter, you can use the Generate function in Intellij

  • By default, every class extends an Object class

    • every time you try to print an object, it will call the toString() method of the object
Person p = new Person(); // 객체(인스턴스) 생성
Component구성 요소Description / 설명
Field필드- Attributes or properties an object has (e.g., name, age, etc.)
- 객체가 가지는 속성 (예: 이름, 나이 등)
Method메서드- Actions or behaviors the object can perform
- 객체가 수행할 수 있는 동작
Constructor생성자- A special method called when the object is created
- 체 생성 시 호출되는 특수한 메서드
Inner Class이너 클래스- A class defined inside another class, used when needed
- 클래스 내부에 정의된 또 다른 클래스 (필요 시 사용)

🧠 Fields and methods represent the state and behavior of an object.

  • state represented by fields
    • the data an object holds
  • behavior represented by methods
    • the actions an objects can perform

Fields

Methods

  • What’s in a method
    • Method signature: method name + parameters
      • addInt(int num1, num2)
      • A good method signature lets you know what the function does & returns at first glance
    • Method body
  • class methods
  • method overloading

Variadic Arguments; varargs

  • Allows a method to accept zero or more arguments of a specified type — like passing an array without needing to create one manually
  • without this, we’ll have to overloading n times
public void printNames(String... names) {
    for (String name : names) {
        System.out.println(name);
    }
}
 
printNames(); // zero arguments
printNames("Alice");
printNames("Bob", "Charlie", "Dave");
  • Conditions
    • Only one varargs parameter is allowed per method
    • It must be the last parameter in the parameter list
  • Internally
    • Java converts to array

Instance

Instance

An instance is a real instance of a class.

  • It has its own data stored in the attributes.
  • You can call methods on the object to perform actions or change its state
  • All instances are objects
Person p1 = new Person();
  • p1 is a reference variable, which stores the address of the heap that has the data
  • You “instantiate” with the new keyword
    • new 쓰는 건 생성자를 호출하는 것
    • new you’re putting the object in the heap memory and putting its address in the reference variable
  • Image overview
    • 즉 같은 클래스로 만든 모든 객체는 동일한 메서드 값을 공유하기 때문에 여러 번 같은 메서드를 선언해 주는 것이 아니라 한 번만 저장해 두고 필요한 경우에만 클래스 영역에 정의된 메서드를 찾아 사용할 수 있는 것입니다.
    • All instances of the same class share the same methods
      • they all reuse the same method stored in the class area