JAVA

객체지향 참조변수 super

연93 2022. 9. 26. 09:59

객체 자식을 가르키는 참조변수. 인스턴스 매서드(생성자)내에만 존재

조상의 멤버를 자신의 멤버와 구별할 때 사용

public class Super {

	public static void main(String[] args) {
		Child c = new Child();
		c.method();
	}
}
class Parent { int x = 10; /* super.x */ }

class Child extends Parent {
	int x = 20;  //this.x
	
	void method() {
		System.out.println("x=" + x);
		System.out.println("this.x=" + this.x);
		System.out.println("super.x=" + super.x);
	}
}
-------------------------------------------------------------
x=20
this.x=20
super.x=10