Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions practice/extends1/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package extends1;

public class Child extends Parent {
public Child() {
System.out.println("Child Constructor");
}

@Override
public void printProtectedName() {
System.out.println("Parent printProtectedName()");
}
}
13 changes: 13 additions & 0 deletions practice/extends1/ChildArg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package extends1;

public class ChildArg extends ParentArg {
public ChildArg() {
super("ChildArgName ~~~ ");
System.out.println("Child Constructor");
}

@Override
public void printName() {
System.out.println("ChildArg printName() Overriding");
}
}
7 changes: 7 additions & 0 deletions practice/extends1/Friend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package extends1;

public class Friend {
public Friend() {
System.out.println("Friend Constructor");
}
}
11 changes: 11 additions & 0 deletions practice/extends1/GrandChild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package extends1;

public class GrandChild extends Child {
public GrandChild() {
System.out.println("GrandChild Constructor");
}

public void print() {
System.out.println("GrandChild!!!!!");
}
}
19 changes: 19 additions & 0 deletions practice/extends1/Parent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package extends1;

public class Parent {
public Parent() {
System.out.println("Parent Constructor");
}

public void printName() {
System.out.println("Parent printName()");
}

private void printPrivateName() {
System.out.println("Parent printPrivateName()");
}

protected void printProtectedName() {
System.out.println("Parent printProtectedName()");
}
}
12 changes: 12 additions & 0 deletions practice/extends1/ParentArg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package extends1;

public class ParentArg {

public ParentArg(String name) {
System.out.println("ParentArg("+name+") Constructor");
}

public void printName() {
System.out.println("ParentArg printName()");
}
}
30 changes: 30 additions & 0 deletions practice/extends1/RunExtends.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package extends1;

public class RunExtends {
public static void main(String[] args) {
// 1. 매개변수가 없는 경우
Child child = new Child();
// child.printName();
//// child.printPrivateName(); 불가능!
child.printProtectedName();

// 2. 매개변수가 있는 경우
ChildArg childArg = new ChildArg();
childArg.printName();

// // 3. 손자의 경우
// GrandChild grandChild = new GrandChild();
// grandChild.printProtectedName();

// 4. 상속 구조가 아닌 경우
Friend friend = new Friend();
// friend.printProtectedName(); 불가능!

// 5. 형변환
// (1) 가능
Parent parent = new Child();

// (2) 불가능
Child child = new Parent();
}
}