• Home
  • About
    • ming photo

      ming

      studying

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Archive
    • All Posts
    • All Tags
    • All categories
  • categories
    • HTML+CSS+JavaScript
    • JAVA
    • Algorithm
    • DB
    • JSP
    • 정보처리기사
    • Spring
    • Thymeleaf
    • 기술면접
  • Projects

JAVA-자바의정석Ch7(객체지향 프로그래밍 2)1~2,3~4,5~6,7~9,10~11

23 Feb 2021

🔶 자바의정석Ch7 - 1~2,3~4,5~6,7~9,10~11

✔ 자바의정석Ch7-1 상속(Inheritance)

- 기존의 클래스로 새로운 클래스를 작성하는 것(코드의 재사용)
- 두 클래스를 부모와 자식으로 관계를 맺어주는 것
- 자손은 조상(부모의 부모)의 모든 멤버를 상속받는다(생성자, 초기화블럭 제외)
- extends 키워드 사용   
- 자손의 멤버 개수는 조상보다  적을 수 없다(같거나 많다)
- 자손의 변경은 조상에 영향을 미치지 않는다 ![ch7-1](https://user-images.githubusercontent.com/65608960/108822642-b4c8c200-7602-11eb-88a8-81edb68e1788.JPG) ![ch7-2](https://user-images.githubusercontent.com/65608960/108822638-b3979500-7602-11eb-9c39-ea9780c65190.JPG)
  • 예제
      package Java_Nam;
          class Tv{
              // 부모 멤버 5개
              boolean power; // 전원상태  (on/off)
              int channel;  // 채널
                
              void power() {
                  power =! power;
              }
              void channelUp() {
                  ++channel;
              }
              void chnnelDown() {
                  --channel;
              }			
          }	
            
          // 자식 멤버 2개 + 부모멤버 5개  = 총 7개
          class SmartTv extends Tv{ // SmartTv는 Ch7_1Ex 캡션(자막)을 보여주는 기능
              boolean caption; // 캡션상태 (on/off)
              void displayCaption(String text) { 
                  if(caption) { // 캡션 상태가 on(true)일 때만 text를 보여준다
                      System.out.println(text);
                  }
              }
          }
    
    
      public class Ch7_1Ex {	
                
              public static void main(String[] args) {
                  SmartTv stv = new SmartTv();// 객체 생성
                    
                  stv.channel = 10; // 조상 클래스로부터 상속받은 멤버
                  stv.channelUp(); // 조상 클래스로부터 상속받은 멤버
                  System.out.println(stv.channel);
                  stv.displayCaption("Hello");
                  stv.caption = true; // 캡션(자막) 기능을 켠다
                  stv.displayCaption("Hello");
                    
          }
                
      }
    

✔ 자바의정석Ch7-3 포함 관계

- 포함(composite) : 클래스의 멤버로 참조변수를 선언하는 것
- 작은 단위의 클래스를 만들고, 이 들을 조합해서 클래스를 만든다 ![ch7-3](https://user-images.githubusercontent.com/65608960/108824397-01ad9800-7605-11eb-8367-06ed20c09526.JPG)

✔ 자바의정석Ch7-4 클래스 간의 관계 결정하기

- 상속관계 : ~은 ~이다(is-a)
- 포함관계 : ~은 ~을 가지고있다(has-a) ![ch7-4](https://user-images.githubusercontent.com/65608960/108824392-007c6b00-7605-11eb-93f7-f32d71c9a31a.JPG)
  • 예제
      package Java_Nam;
    
          class Point{
              int x;
              int y;
          }
          //class Circle extends Point{ // 상속
          //	int r;
          //}
    
          class Circle { // 포함
                
              Point p = new Point(); // 참조변수의 초기화
              int r;
          }
    
          public class Ch7_4Ex_InheritanceTest {
    
              public static void main(String[] args) {
                    
                  Circle c = new Circle(); // 객체 생성
                    
                  // circle 객체 멤버 접근
                  c.p.x =1;
                  c.p.y =2;
                  c.r =3;
                    
                  System.out.println("c.p.x = " + c.p.x);
                  System.out.println("c.p.y = " + c.p.y);
                  System.out.println("c.r = " + c.r);
          }
    
      }
    

✔ 자바의정석Ch7-5 단일 상속 (Single Inheritance)

- java는 단일상속만 허용한다 => 하나의 부모만 상속 (C++은 다중상속 허용)
- 비중이 높은 클래스 하나만 상속관계로, 나머지는 포함관계로 한다

✔ 자바의정석Ch7-6 Object클래스 : 모든 클래스의 최고 조상

- 부모가 없는 클래스는 자동적으로 Object클래스를 상속받게 된다
- 모든 클래스는 Object에 정의된 11개의 메서드를 상속받는다 : toString(),equals(Object obj),hashCode()...

✔ 자바의정석Ch7-7 메서드 오버라이딩(overriding)

- 상속받은 조상의 메서드를 자신에 맞게 변경하는 것 ![ch7-7](https://user-images.githubusercontent.com/65608960/108838681-4f330080-7617-11eb-8ba2-cf4016dd074f.JPG)

✔ 자바의정석Ch7-8 오버라이딩의 조건

1) 선언부가 조상 클래스의 메서드와 일치해야 한다
2) 접근제어자를 조상 클래스의 메서드보다 좁은 범위로 변경할 수 없다
3) 예외는 조상 클래스의 메서드보다 많이 선언할 수 없다 ![ch7-8](https://user-images.githubusercontent.com/65608960/108838674-4e01d380-7617-11eb-8595-efb57a2b568b.JPG)

✔ 자바의정석Ch7-9 오버로딩 vs 오버라이딩

- 오버로딩(overloading) : 기존에 없는 새로운 (이름이 같은)메서드르 정의하는 것(new)
- 오버라이딩(overrideing) : 상속받은 메서드의 내용을 변경하는 것(change,modify) ![ch7-9](https://user-images.githubusercontent.com/65608960/108838685-4f330080-7617-11eb-9745-9ba21034582c.JPG)

✔ 자바의정석Ch7-10 참조변수 super

- 객체 자신을 가리키는 참조변수, 인스턴스 메서드(생성자) 내에서만 존재한다 -> static메서드 내에서 사용 불가
- 조상의 멤버를 자신의 멤버와 구별할 떄 사용한다

✔ 자바의정석Ch7-11 super() : 조상의 생성자 => 참조변수 super 랑 관련 없다

- 조상의 생성자를 호출할 때 사용한다
- 조상의 멤버는 조상의 생성자를 호출해서 초기화 한다
- 생성자의 첫 줄에는 반드시 생성자를 호출해야 한다 => 그렇지 않으면 컴파일러가 생성자의 첫 줄에 super();를 삽입한다 ![ch7-11](https://user-images.githubusercontent.com/65608960/108840120-3e838a00-7619-11eb-969e-57854570b826.JPG) ![ch7-11_2](https://user-images.githubusercontent.com/65608960/108840118-3deaf380-7619-11eb-8469-065c7a8c396c.JPG) ![ch7-11_3](https://user-images.githubusercontent.com/65608960/108840110-3cb9c680-7619-11eb-8f15-822350f6d761.JPG)


Share Tweet +1