- ์ ๊ทผ ์ ์ด์ (Access Modifiers)
- private: ํด๋น ํด๋์ค ๋ด๋ถ์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
- default: ๊ฐ์ ํจํค์ง ๋ด์์๋ง ์ ๊ทผ ๊ฐ๋ฅ /- ์๋ ๋๋ถ๋ถ ์์ฝ ํ ์ ์์ด์ default์ฐ๋ ๋ฏ/
- protected: ๊ฐ์ ํจํค์ง ๋๋ ํ์ ํด๋์ค์์ ์ ๊ทผ ๊ฐ๋ฅ
- public: ์ด๋์๋ ์ ๊ทผ ๊ฐ๋ฅ
private < default < protected < public
- ์์ฑ์ (Constructors)
- ๊ธฐ๋ณธ ์์ฑ์ (No-Arg Constructor) - ์ด๊ธฐํ, ํด๋์ค๋ช ์ด๋ ๊ฐ์์ผํด.
- ๋งค๊ฐ๋ณ์ ์์ฑ์ (Parameterized Constructor) - ์์ฑ์ฌ(์์ ๋ค์ด์์) ์ด๋ฐ ํ์์ด๋๊น๋งค๊ฐ๋ณ์ ๊ฐ์ง๊ณ ์์
- ์์ฑ์ ์ค๋ฒ๋ก๋ฉ - ๋งค๊ฒจ๋ณ์ ๋ฃ๊ฑฐ๋ ์๋ฃ๊ฑฐ๋ ์์ฑ์ ์ด๋ฆ ๋๊ฐ์ด ์จ๋ ๋จ. ๋จ ๊ตฌ๋ถ ๋จ.
- this ํค์๋
- ๋ฉค๋ฒ ๋ณ์์ ๋งค๊ฐ๋ณ์์ ๊ตฌ๋ถ - ๋์ฒด๋ก this ํค์๋๋ ์์ int age ์ฒ๋ผ ์์ ๊ฐ์ผ๋ก ๊ฐ๋ ๊ฒ ๊ฐ์.
- NumberPair(int value) {
this(value, value); // this()๋ฅผ ์ฌ์ฉํ์ฌ ์์ (int, int) ์์ฑ์๋ฅผ ํธ์ถ (value๋ฅผ ๋ ๋ฒ ์ ๋ฌ)
} - ๋ค๋ฅธ ์์ฑ์ ํธ์ถ
- static ํค์๋
- static ๋ณ์ (ํด๋์ค ๋ณ์) - static ๋ณ์๋ ์ด์ ์ด๋์๋ ์ ๊ทผ ๊ฐ๋ฅํ๊ฒ ๋๋ค. public static int count = 0;
- static ๋ฉ์๋ (ํด๋์ค ๋ฉ์๋) - ์๋๋ฉด newํด์ Calculator calc = new Calculator(); ์ด๋ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผํ๋๋ฐ ๊ทธ๊ฑธ ํ๊ธฐ ๊ท์ฐฎ์ผ๋ฉด / ๊ทธ๋ฆฌ๊ณ main์ ์ธ์ ๋ static์ด ํฌํจ๋์ด ์ด์จ
public class MathUtil {
public static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
int result = MathUtil.add(5, 3); // ๊ฐ์ฒด ์์ฑ ์์ด ํธ์ถ
System.out.println(result); // ์ถ๋ ฅ: 8
}
}
- final ํค์๋
- final ๋ณ์ - ํ ๋ฒ ๊ฐ์ด ์ค์ ๋๋ฉด ๋ณ๊ฒฝํ ์ ์๋ค. (์ด๋์๋ ์์ ๋๋ฉด ์๋๋ ์ ๋ฅผ ์ฐ๊ธฐ ์ข๋ค.)
- final ๋ฉ์๋์ ํด๋์ค
- instanceof ์ฐ์ฐ์ - instanceof ํค์๋๋ "๊ฐ์ฒด์ ํ์ ์ ๊ฒ์ฌํ ๋" / ํธ๋ฃจ? ํ์ค? /
- private๊ณผ getter/setter์ ๊ฐ๋ ์ ๋ฆฌ - ํ๋ผ์ด๋น์ ๊ฒฝ์ฐ. ๋ค๋ฅธ ํด๋์ค์์ ์ธ ์๋ ์๋๋ฐ getter์๊ฐ์ ์ฝ์๋ setter๋ ๊ฐ์ ๋ณ๊ฒฝํ ๋ ์ฌ์ฉํ๋ค.
- static ํค์๋์ ํ์ฉ ๋ชฉ์ -
- ์์ฑ์์ ์ ๊ทผ ์ ์ด์(public/private)
- static ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์ด์
- ์์(Inheritance) - ์์์ ๊ฒฝ์ฐ ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํฉ๋๋ค.
- ์ค๋ฒ๋ผ์ด๋ฉ(Overriding) - @Override๋ฅผ ์จ์ผํฉ๋๋ค. ๋ถ๋ชจ์ ๊ฒ์ ํธ์งํด์ ์ธ ์ ์๋ค.
- super ํค์๋
- super๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ชจ ๋ฉ์๋ ํธ์ถํ๊ธฐ - class Parent {
public void sayHello() {
System.out.println("Hello from Parent!");
}
}
class Child extends Parent {
@Override
public void sayHello() {
super.sayHello(); // ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ๋ช ์์ ์ผ๋ก ํธ์ถ
System.out.println("Hello from Child!");
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.sayHello(); // ๋ถ๋ชจ + ์์ ๋ฉ์๋ ๋ชจ๋ ์คํ
}
}
์ด๋ ๊ฒ ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ๋ช ์์ ์ผ๋ก ํธ์ถํด์ผ ํฉ๋๋ค. ์ฌ๊ธฐ์ super. ์ ๋ถ๋ชจ๋ฅผ ๋ปํฉ๋๋ค. - super()๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ชจ ์์ฑ์ ํธ์ถํ๊ธฐ - - super(baseSalary); // ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ / ๋ง์ฝ ์ด๋ ๊ฒ ๋๋ฉด ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํ๊ฒ ๋๋ค.์ฆ ์ด๊ธฐํ ๊ฐ๋ฅ.
- super๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ชจ ๋ฉ์๋ ํธ์ถํ๊ธฐ - class Parent {
- ์ธํฐํ์ด์ค (Interface)
- ์ธํฐํ์ด์ค์ ํน์ง
// ์ธํฐํ์ด์ค ์ ์ธ
interface Animal {
void makeSound(); // ์ถ์ ๋ฉ์๋ (๊ตฌํ ์์)
}
// ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("๋ฉ๋ฉ!");
}
}
// ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋ ๋ค๋ฅธ ํด๋์ค
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("์ผ์น!");
}
}
// ์คํ ํด๋์ค
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
Cat myCat = new Cat();
myDog.makeSound(); // ์ถ๋ ฅ: ๋ฉ๋ฉ!
myCat.makeSound(); // ์ถ๋ ฅ: ์ผ์น!
}
}
-/์์ ๊ฒ์๋ณด๋ฉด ์ ์ ์๋ฏ์ด ๋ญ์ ๋ ๋ฐ interface Animal ํ๋ฉด์ ์ ์ธํ๊ณ ์์ void makeSound() ๋ผ๊ณ ์ถ์ ๋ฉ์๋ ๋ผ๊ณ ์ ์ด์ ์ ์ธ ๋ ๋ค ๋จผ์ ํด๋๊ณ ๋์ค์ ์ด๊ฑธ ํธ๋ผ์ฉํด์ ํด๋์ค ๊ตฌํํ๋ผ๋ ์๋ฆฌ/ ๋ฉ์๋ ์ด๋ฆ ์ผ์ผํ ๋ฐ๋ก ์ง์ ํ์์๊ณ ํธํจ. - ์ธํฐํ์ด์ค์ ๋ค์ค ๊ตฌํ
interface Movable {
void move(int x, int y); // "์ด๋" ๊ธฐ๋ฅ ์ฝ์!
}
interface Resizable {
void resize(double factor); // "ํฌ๊ธฐ ๋ณ๊ฒฝ" ๊ธฐ๋ฅ ์ฝ์!
}
์ด๋ ๊ฒ ์ฌ๋ฌ๊ฐ ๊ตฌํํ ์ ์์ต๋๋ค.
- ์ธํฐํ์ด์ค์ ํน์ง
- ๋คํ์ฑ (Polymorphism)
- ๋คํ์ฑ์ ํ์์ฑ
interface Animal {
void makeSound();
}
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("๋ฉ๋ฉ!");
}
}
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("์ผ์น!");
}
}
public class Main {
public static void main(String[] args) {
Animal[] animals = { new Dog(), new Cat() };
for (Animal a : animals) {
a.makeSound(); // ๊ฐ์ ๋ฐฉ์์ผ๋ก ํธ์ถ ๊ฐ๋ฅ
}
}
}
//์์์ ์ธํฐํ์ด์ค ์ด์ฉํด์ ๋์ค์ ์ ์ธํด์ ์ผ๋๊ฒ ์ฒ๋ผ ์ฌ๋ฌ๋ฒ ์ธ ์ ใ ฃใ ใ ์ - ๋คํ์ฑ์ ํต์ฌ ๊ธฐ๋ฅ
- ๋คํ์ฑ์ ์ฅ์
- ๋คํ์ฑ์ ํ์์ฑ
- ์ถ์ ํด๋์ค (Abstract Class)
- ์ถ์ ํด๋์ค์ ํน์ง -
- ๊ณตํต๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉด์๋
- ์ผ๋ถ ๊ธฐ๋ฅ์ ์์ ํด๋์ค์์ ๋ฐ๋์ ๊ตฌํํ๋๋ก ๊ฐ์ ํ๋ ํด๋์ค์ด๋ค.
์ถ์ ํด๋์ค๋ abstract ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธ๋๋ฉฐ, ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ ์ ์๋ค.
์์ ํด๋์ค์์ ์์๋ฐ์ ์ฌ์ฉํด์ผ ํ๋ค.
- ์ถ์ ํด๋์ค์ ์ผ๋ฐ ํด๋์ค์ ์ฐจ์ด์
// ์ถ์ ํด๋์ค (๋ถ๋ชจ)
abstract class Vehicle {
// ๊ณตํต์ผ๋ก ์ฐ์ด๋ ๊ธฐ๋ฅ
void stop() {
System.out.println("์ฐจ๋์ด ๋ฉ์ท์ด์.");
}
// ๊ฐ ์ฐจ๋๋ง๋ค ๋ค๋ฅด๊ฒ ๊ตฌํํด์ผ ํ ๊ธฐ๋ฅ
abstract void start();
}
// ์๋์ฐจ ํด๋์ค (์์)
class Car extends Vehicle {
void start() {
System.out.println("์๋์ฐจ๊ฐ ๋ถ๋ฆ๋ถ๋ฆ~");
}
}
// ์คํ ๋ฐ์ด ํด๋์ค (์์)
class Motorcycle extends Vehicle {
void start() {
System.out.println("์คํ ๋ฐ์ด๊ฐ ๋ถ์์~");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle motorcycle = new Motorcycle();
car.start(); // ์ถ๋ ฅ: ์๋์ฐจ๊ฐ ๋ถ๋ฆ~
car.stop(); // ์ถ๋ ฅ: ์ฐจ๋์ด ๋ฉ์ท์ด์.
motorcycle.start(); // ์ถ๋ ฅ: ์คํ ๋ฐ์ด๊ฐ ๋ถ์์~
motorcycle.stop(); // ์ถ๋ ฅ: ์ฐจ๋์ด ๋ฉ์ท์ด์.
}
}
-/ ์ด๊ฑธ ๋ณด๋ฉด ์ ์ ์๋ฏ์ด abstract ๋ก ์ฐ์ธ class๋ ๊ณตํต์ด๋๊ณ abstract๋ก ์ฐ์ธ ๋ณ์๋ ๊ฐ ์ฐจ๋๋ง๋ค ๋ค๋ฅด๊ฒ ์ฐ์ผ ์ ์๊ฒ ๋์ด ์๋ค. ์ถ์ํด๋์ค!
- ์ถ์ ํด๋์ค์ ํน์ง -
- ์ธํฐํ์ด์ค์ ์ถ์ ํด๋์ค ๋น๊ต
- ๋์์ธ ํจํด (Design Pattern)
- ์ฑ๊ธํค ํจํด (Singleton Pattern)
- ์ธ๋๊ฐ ๋งํ๊ธธ... ๊ฐ๋ฐ์๋ค์ ๋๋ฌผ๋ก ๋ง๋ค์ด์ง ๊ธฐ์ค, ํ์ค, ์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค. (์ด๋ ๊ฒ ํ์์ค..)
- ์ฑ๊ธํค ํจํด (Singleton Pattern)
- ์ ๋ค๋ฆญ (Generics)
- ์ ๋ค๋ฆญ ํด๋์ค (Generic Class)
- ์ ๋ค๋ฆญ ๋ฉ์๋ (Generic Method)
- ์ ํ๋ ํ์ (Bounded Type Parameters)
- ์์ผ๋์นด๋ (Wildcard)
- ์ ๋ค๋ฆญ์ ์ฅ์
- ํ์ ์์ ์ฑ(Type Safety) ๋ณด์ฅ
- ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ ์ฆ๊ฐ
- ํ ๋ณํ ํ์ ์์ (๋ถํ์ํ ์บ์คํ ์ ๊ฑฐ)
List<String> stringBox = new ArrayList<>(); // ๋ฌธ์์ด๋ง ๋ด๋ ์์
์ด๊ฑฐ๋ ๋ฌธ์์ด๋ง ๋ค์ด๊ฐ์ง ์ซ์๋ ๋ชป๋ค์ด๊ฐ๋ค.
<T> ๋ผ๊ณ ํ๋ฉด ๋์ค์ ํ์ด ์ ํด๋ ๋จ. <T extends Number> ๋ ์ซ์์ ๋ง ๊ฐ๋ฅํ ์ ํ๋ ํ์
๋งํจ
class Util {
// <T>๋ ๋์ค์ ๊ฒฐ์ ํ ํ์
!
public static <T> void print(T value) {
System.out.println(value);
}
}
public class Main {
public static void main(String[] args) {
Util.print(10); // ์ซ์ ์ถ๋ ฅ
Util.print("์๋
!"); // ๋ฌธ์์ด ์ถ๋ ฅ
Util.print(3.14); // ์ค์ ์ถ๋ ฅ
}
}
Java์ ํด๋์ค์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP) ๊ฐ๋ ์ ์ ๋ฐ์ ์ผ๋ก ์ ๋ค๋ฃจ๊ณ ์์ต๋๋ค.