Skip to content

Commit

Permalink
[Week2][Chap9] 상속과 접근 제어 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Jan 16, 2024
1 parent 1696cbb commit a57f9a1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 김아린/java-basic/src/extends1/access/ExtendsAccessMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package extends1.access;

import extends1.access.child.Child;

public class ExtendsAccessMain {
public static void main(String[] args) {
Child child = new Child();

child.call();
}
}
19 changes: 19 additions & 0 deletions 김아린/java-basic/src/extends1/access/child/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package extends1.access.child;

import extends1.access.parent.Parent;

public class Child extends Parent {
public void call() {
publicValue = 1;
protectedValue = 1; // 상속 관계 or 같은 패키지에서 사용 가능
// defaultValue = 1; // 다른 패키지에 접근 불가 -> 컴파일 오류
// privateValue = 1; // 접근 불가, 컴파일 오류

publicMethod();
protectedMethod();
// defaultMethod(); // 다른 패키지에 접근 불가 -> 컴파일 오류
// privateMethod(); // 접근 불가, 컴파일 오류

printParent();
}
}
33 changes: 33 additions & 0 deletions 김아린/java-basic/src/extends1/access/parent/Parent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package extends1.access.parent;

public class Parent {
public int publicValue;
protected int protectedValue;
int defaultValue;
private int privateValue;

public void publicMethod() {
System.out.println("Parent.publicMethod");
}
protected void protectedMethod() {
System.out.println("Parent.protectedMethod");
}
void defaultMethod() {
System.out.println("Parent.defaultMethod");
}
private void privateMethod() {
System.out.println("Parent.privateMethod");
}

public void printParent() {
System.out.println("==Parent 메서드 완==");
System.out.println("publicValue = " + publicValue);
System.out.println("protectedValue = " + protectedValue);
System.out.println("defaultValue = " + defaultValue);
System.out.println("privateValue = " + privateValue);

// 부모 메서드 안에서 모두 접근 가능
defaultMethod();
privateMethod();
}
}

0 comments on commit a57f9a1

Please sign in to comment.