1. 객체지향의 주요 특성.
 1) 추상화 (Abstraction)
 2) 캡슐화 (Encapsulation)
 3) 상속화 (Inheritance)
 
 4) 다형성 (Polymorphism)
     1. Primitive Type
      1) implicit conversion
      2) explicit conversion
     2. Reference Type
      1) 서로 다른 객체는 강제적, 암시적 형변환 안된다.

 public class CastDemo {
 public static void main(String[] args) {
  Test td = new Test();
  Demo d = new Demo();
  td = (Test)d;  // 오류
 }
}

class Test{
 int su = 5;
}
class Demo{
 String name = "taehyung";
}


      2) 자식 --> 부모로 형변환 : 강제적, 암시적 모두 성공한다.
      3) 부모 --> 자식으로 형변환 : 강제적 형변환만 가능하고, 런타임 때 오류발생가능 (반드시 instanceof()로 검증해야한다.)

 public class CastDemo {
 public static void main(String[] args) {
  Test t = new Test();
  Demo d = new Demo();
  t = d;   //자동 형변환
  t = (Test)d;  // 강제 형변환
 }
}
class Test{
 int su = 5;
}
class Demo extends Test{  // 상속
 String name = "taehyung";
}

 public class CastDemo {
 public static void main(String[] args) {
  Test t = new Test();
  Demo d = new Demo();
  d = t;  // 자동 형변환 오류
  d = (Demo)t;  //강제 형변환은 된다
 }
}
class Test{
 int su = 5;
}
class Demo extends Test{
 String name = "taehyung";
}
 public class CastDemo {
 public static void main(String[] args) {
  Test t = new Test();
  Demo d = new Demo();
  if (t instanceof Demo) d = (Demo)t;
  else System.out.println("cannot");
 }
}
class Test{
 int su = 5;
}
class Demo extends Test{
 String name = "taehyung";
}
 자식 --> 부모  부모 -->자식  부모 --> 자식(instanceof())
 
 
 
 
ex) 포유류 형을 상속받은 개나 고양이 들의 형태를 다시 포유류로 형변환이 된다.

 void display(int choice){
  Mammal m = null;   // 경우에 따라 자식의 형태가 부모형으로 변환되어 들어간다.
  switch (choice) {
  case 1: m = new Dog();   break;
  case 2: m = new Cat();  break;
  case 3: m = new Korean();  break;
  case 4: m = new American();  break;
  default:System.out.println("누구냐 넌,"); break;
  }
  m.saySomething();  // 각 종족에 따른 울음소리를 실행하는 메소드

 

ex2) 카센터 자동차 수리

 public class CarCentar {
 public static void main(String[] args) {
  CarCentar cc = new CarCentar();
  Matiz m = new Matiz("마티즈");
  Carnival c = new Carnival("카니발");
  Sonata s = new Sonata("소나타");
  cc.repair(m);
 }
 void repair(Car c){   //부모형 변수에 자식들의 입력을 자동으로 받는다.
  if (c instanceof Sonata) { // 형태를 검사한다.
   System.out.println("현대차 ");
  }else if(c instanceof Matiz){
   System.out.println("대우차 ");
  }else if(c instanceof Carnival){
   System.out.println("기아차 ");
  }  
  System.out.println(c.getName()+" 수리완료!"); //들어온 형태에 상관없이 getName메소드를 사용할수있다.
 }
}

 

 

 

 

 

 

ps -------------------------------------------------------------------------------     

1. 1) tightly coupled(Early Binding) : 컴파일 때.

   2) loosely coupled(Late binding) : 런타임 때

 

 

2. Overriding 할 때 자식은 부모보다 권한이 작으면 안된다.

 private < default < protected < public

 

1. 객체지향의 주요 특성.
 1) 추상화 (Abstraction)
 2) 캡슐화 (Encapsulation)

 3) 상속화 (Inheritance)
  - 자바는 단일 상속으로 1개의 상위 클래스로부터 상속받을 수 있다. (역학 조사로 Maintanance에 좋지만 현실적으로 단일 상속을 사용하지 않는다. extends는 한번 사용하지만, 인터페이스(가상클래스)를 활용한다.)

 

C++ 

Java 

C# 

부모

 Base

Super 

base 

 표현

  :

 extends 

 

 자식

 Derived

 다중상속

 sub

단일상속

 

 

 
  - TwoD extends ThreeD : 생성자로 TwoD를 생성할때 먼저 부모클래스인 ThreeD를 메모리 heap에 올린다.
  - 자식 주소로 부모를 접근하면 상속, 부모 주소로 자식을 접근하면 다형성 이다.
  - class와 class, interface와 interface 간에는 extends, class와 inteface는 implements(동종간에는 extends,이종간에는 implements)
  - 관계    
    i) "is a"  : ex) 오토바이 -> 탈것
    ii) "has a"  ex) 주방 -> 냉장고
  - 상속이 되지 않는 것.
    i) private 선언 된 것은 상속되지않는다.
    ii) static 역시 상속되지 않는다.
    iii) 생성자는 상속되지 않는다.
       - 자식이 부모를 생성하려면 this메소드와 같은 형식의 super()를 사용한다. (마찬가지로 첫줄에 쓴다)
    iV) Overshadow 변수(클래스 변수와 같은 이름의 변수를 사용), Override 메소드 는 상속되지않는다. 자식 것을 쓴다.
       - super()를 사용하여 해결 할 수 있다.
       - String 형은 자체적으로 static 이기 때문에 this()나 super()이용해 접근할 수 없다.
 4) 다형성 (Polymorphism)

 
 
 
ps.----------------------------------------------------------------------------------------
- Object는 모든 객체들의 필수 요소의 집합이다. (원형이다.)
 
- 객체 비교에 대하여
   1. ==, !=
     1) value type -> 값비교, (su==num)
     2) Reference type -> 주소비교
     3) String type --> 값비교(new 없을 때), 주소비교(new 사용) //보통 String은 equals()를 사용한다.
   2. equals()
     1) value type --> 해당없음 (su.equals(num))
     2) Reference type --> 주소비교
     3) String type --> new와 관계없이 값비교
 

+ Recent posts