์ปดํจํฐ ์ธ์ด๋ ํฌ๊ฒ ์ ์ฐจํ(Precedural Program Language)๊ณผ ๊ฐ์ฒด์งํฅ(Object Oriented Program Language)์ด ์๋๋ฐ, Java์ ํน์ง ์ค ํ๋๊ฐ ๋ฐ๋ก ๊ฐ์ฒด์งํฅ ์ธ์ด๋ผ๋ ์ ์ด๋ค. ๊ฐ์ฒด ์งํฅ ์ธ์ด์ ์ ์ผ ํฐ ์ฅ์ ์ ํ์ ํ๊ธฐ ํธ๋ฆฌํ๋ค๋ ์ ์ด๋ค.
1. ์บก์ํ(Encapsulation)
๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํํด์ ์ธ์คํด์ค ๊ฐ ์๋ก ์ํฅ์ ์ ์ฃผ๊ณ ๋ ๋ฆฝ์ ์ผ๋ก ์ด์ํ๋ค. ๊ฐ์ฒด๋ฅผ ๋ด๋ถ๋ฅผ ๋ณดํธํ๊ณ ๋ณผ ์ ์๊ฒ ํ์ฌ ์ ๋ณด ์๋์ด ๊ฐ๋ฅํ๊ฒ ํ๋ค.
2. ์์์ฑ(Inheritance)
๋ค๋ฅธ ํด๋์ค(๋ถ๋ชจ ํด๋์ค)์ ํน์ฑ์ ๊ทธ๋๋ก ๋ฌผ๋ ค๋ฐ๋ ๊ฒ์ผ๋ก, ์ฝ๋์ ์ค๋ณต์ฑ์ ์ต๋ํ ์ ๊ฑฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
์์ ์ฝ๋๋ฅผ ํตํด ์ดํดํด๋ณด์
์์ ์ฝ๋
class Parent {
int a = 3;
int b = 4;
public void parentPrint(){
System.out.println("Parent Class");
}
}
class Child extends Parent{ //์์: extends ๋ถ๋ชจํด๋์ค์ด๋ฆ
int c = 5;
int d = 6;
public void childPrint(){
System.out.println("Child Class");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Child child = new Child();
child.parentPrint();
child.childPrint();
System.out.println("a = " + child.a);
}
}
console
Parent Class
Child Class
a = 3
Child ํด๋์ค์๋ parentPrint ๋ฉ์๋์ int a, int b ๊ฐ์ด ์์ง๋ง, Parent ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ์ถ๋ ฅ์ํฌ ์ ์์์ ๋ณผ ์ ์๋ค.
์๋ฐ๋ ๋ค์ค์์์ด ์๋๋ค, ๋ค๋ง ํด๋์ค์ ์ธํฐํ์ด์ค๋ฅผ ๋์์ ์์๋ฐ์ ์๋ ์๋ค.
3. ๋คํ์ฑ(Polymorphism)
๋คํ์ฑ์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ผ์ ํ๋ ํจ์๋ฅผ ๋์ผํ ์ด๋ฆ์ผ๋ก ํธ์ถํด ์ฃผ๋ ๊ฒ์ ๋งํ๋ค.
์ค๋ฒ๋ผ์ด๋ฉ(Overriding)์ ๋คํ์ฑ์ ํต์ฌ ๊ฐ๋ ์ค ํ๋๋ก, ๊ฐ์ ์ด๋ฆ์ ๋ฉ์๋๋ฅผ ๋ค์ํ ํด๋์ค์์ ๋ค๋ฅด๊ฒ ๊ตฌํํ ์ ์๋๋ก ํด์ค๋ค. ์ด๋ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์ ํด๋์ค์์ ํ์์ ๋ง๊ฒ ์์ ํ ์ ์๊ฒ ํด์ค๋ค. ์ฆ, ์ ์ฐ์ฑ๊ณผ ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ์ ๋์ธ๋ค.
์์ ์ฝ๋
class Family{
public void hello(){
System.out.println("Hello everyone"); //๊ฐ์๋ฉ์๋ํ: ์คํ์ ์ํจ
}
}
class KimFamily extends Family{
@Override
public void hello(){
System.out.println("Hello Kim");
}
}
class ParkFamily extends Family{
@Override
public void hello(){
System.out.println("Hello Park");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Family f;
f = new Family();
f.hello();
f = new KimFamily();
f.hello();
f = new ParkFamily();
f.hello();
}
}
console
Hello everyone
Hello Kim
Hello Park
์์์์ ๋ณด์ด๋ฏ์ด ๊ฐ์ hello ๋ฉ์๋๋ฅผ ํธ์ถํ์ง๋ง, ๋ค๋ฅธ ๊ฐ์ด ์ถ๋ ฅ๋์๋ค.
class Animal{
public void move(){
System.out.println("Animal is moving");
}
}
class Human extends Animal {
@Override
public void move(){
System.out.println("Human is walking");
}
}
class Dog extends Animal {
@Override
public void move(){
System.out.println("Dog is running");
}
}
class Bird extends Animal {
@Override
public void move(){
System.out.println("Bird is flying");
}
}
public class PolymorphismExample2 {
public static void main(String[] args) { //๋ฉ์ธ ๋ฉ์๋๋ ํธ์์ ์ด๋ค ํด๋์ค ๋ด์ ๋ฉ์๋๋ก ๋ฑ๋ก์ ๋์ง๋ง, ๋ฉ์ธ ๋ฉ์๋๋ฅผ ํ๊ณ ์๋ ํด๋์ค์๋ ํ๋ฑ์ ๊ด๊ณ๊ฐ ์๋ค๊ณ ๋ด
PolymorphismExample2 animalTest = new PolymorphismExample2();
animalTest.moveAnimal(new Human());
animalTest.moveAnimal(new Dog());
animalTest.moveAnimal(new Bird());
}
public void moveAnimal(Animal animal){
animal.move();
}
}
console
Human is walking
Dog is running
Bird is flying
4. ์ถ์ํ(Abstract)
์ถ์ํ๋ณด๋ค๋ ์์ฝ์ด๋ผ๊ณ ๋ด์ผ ํ๋ค(์ค์ญ)
4-1. abstract class (๊ธฐ์กด ํด๋์ค + ์ค๊ณ๋)
์์ ์ฝ๋
abstract class SalesPlan {
public void companyGoal(){
System.out.println("2023๋
์ ์ฐ ๋ชฉํ 300์ต ๋ฌ์ฑ!");
}
abstract public void departGoal(); //์ถ์ ๋ฉ์๋(abstract method)
abstract public void product();
}
class ATeam extends SalesPlan {
public void manager(){
System.out.println("A ํ์ฅ: hanav");
}
@Override
public void departGoal() {
System.out.println("A Team ํ๋งค ๋ชฉํ 70์ต ๋ฌ์ฑ!");
}
@Override
public void product() {
System.out.println("A Team ์ฃผ๋ ฅ ์ํ : TV");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
ATeam aTeam = new ATeam();
aTeam.manager();
aTeam.companyGoal();
aTeam.departGoal();
}
}
console
A ํ์ฅ: ๊น๋ฏผ์
2023๋
์ ์ฐ ๋ชฉํ 300์ต ๋ฌ์ฑ!
A Team ํ๋งค ๋ชฉํ 70์ต ๋ฌ์ฑ!
4-2. interface (์ค๊ณ๋)
์์ ์ฝ๋
interface SalesPlanInterface { //์์ ํ ์ค๊ณ๋(์์ฝ)
public void manager();
public void goal();
public void product();
}
class ATeam1 implements SalesPlanInterface {
@Override
public void manager() {
System.out.println("A1 ํ์ฅ: ๊น๋ฏผ์");
}
@Override
public void goal() {
System.out.println("A Team1 ํ๋งค ๋ชฉํ 70์ต ๋ฌ์ฑ!");
}
@Override
public void product() {
System.out.println("A Team1 ์ฃผ๋ ฅ ์ํ : TV");
}
}
public class InterfaceExample{
public static void main(String[] args) {
ATeam1 aTeam = new ATeam1();
aTeam.manager();
aTeam.goal();
aTeam.product();
}
}
console
A1 ํ์ฅ: ๊น๋ฏผ์
A Team1 ํ๋งค ๋ชฉํ 70์ต ๋ฌ์ฑ!
A Team1 ์ฃผ๋ ฅ ์ํ : TV
'๐ฅ๏ธ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Generic ๊ธฐ๋ฒ (0) | 2023.03.22 |
---|---|
[Java] try ~ catch ~finally ์์ธ์ฒ๋ฆฌ (0) | 2023.03.22 |
[Java] Optional ๊ฐ์ฒด : ifPresent(), isPresent(), orElse(), map() (0) | 2023.03.22 |
[Java] ๋ถ๋ณ ๊ฐ์ฒด์ Wrapper Class (0) | 2023.03.22 |
[Java] Stream API: stream์ ๋ค์ํ ์ฐ์ฐ๊ณผ ์์ ์ฝ๋ (0) | 2023.03.21 |