Parent.java
public class Parent{
String name="Parent";
public void showInfo(){
System.out.println("부모 클래스 입니다.");
}
}
Child.java
public class Child extends Parent{
String name= "Child";
public void showInfo(){
System.out.println("자식 클래스입니다.");
}
}
Main.java
public class Main {
public static void main(String[] args){
Parent parent =new Child();
System.out.println(parent.name);
parent.showInfo();
//Parent
//자식 클래스입니다.
}
}
public class Parent{
String name = "Parent";
}
public class Child extends Parent{
String name = "Child";
}
public class Main {
//Variable Hiding
Child child = new Child();
System.out.println(child.name); //Child
Parent parent = new Parent();
System.out.println(parent.name); //Parent
Parent parent2 = new Child();
System.out.println(parent2.name); //Parent
}
class Parent{
int var;
public int plus() {
return ++var;
}
public int multi() {
return var=var*var;
}
}
class Child extends Parent {
String var;
}
Reference: