'Java > Overriding' 카테고리의 다른 글
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
|---|---|
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
|---|---|
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
package com.over3;//super에러 해결하기
public class PointTest {
public static void main(String[] args){
Point3D p3 = new Point3D(1,2,3);
System.out.println(p3.getLocation());
}
}
//Point(){}
class Point{
int x;
int y;
String getLocation(){
return "x :"+x+", y :"+y;
}
}
class Point3D extends Point{
int z;
Point3D(int x, int y, int z){
/*2.에러 해결방법
부모 클래스에 default 생성자가 없고
인자를 전달해야 하는 생성자만 있을경우
명시적으로 super(인자)를 이용해서 부모클래스 생성자 호출
super(x,y);*/
this.x = x;
this.y = y;
this.z = z;
}
String getLocation(){//오버라이딩
return "x : " +x+", y :"+y+"m z :" + z;
}
}
x : 1, y :2m z :3
| 예비 (0) | 2012.04.11 |
|---|---|
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
package com.over2;//super로 은닉된 슈퍼클래스의 멤버변수 접근하기
class Point2D{
protected int x=10; //은닉 변수
protected int y=20; //혹은 쉐도우 변수
}
class Point3D extends Point2D{
protected int x=40; //슈퍼 클래스에 존재하는 멤버변수를
protected int y=50; //서브 클래스에 다시 한번 정의함
protected int z=30;
public void print(){
System.out.println(x+", "+y+", "+z);
}
public void print02(){
System.out.println(super.x+", "+super.y+", "+z);
}
}
public class SuperTest04 {
public static void main(String[] args){
Point3D pt = new Point3D();
pt.print();
pt.print02();
}
}
40, 50, 30
| 예비 (0) | 2012.04.11 |
|---|---|
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
package com.over2;//super 기본정보
class Parent2{
int a=100;
public Parent2(int a){
this.a = a;
}
}
class Child2 extends Parent2{
int b=200;
public Child2(){
//부모 클래스가 default 생성자만 사용할 경우
//super();//명시하지는 않지만 암묵적으로
//부모 클래스의 default 생성자 호출
//부모 클래스가 default 생성자를 사용하지 않고
//인자가 전달되는 생성자를 명시했을때
super(200);
}
}
public class Exten03 {
public static void main(String[] args){
Child2 ch = new Child2();
System.out.println(ch.a);
System.out.println(ch.b);
}
}
200
200
| 예비 (0) | 2012.04.11 |
|---|---|
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
package com.over2;//메소드 오버라이딩 super참조 변수
class A{
public void make(){
System.out.println("부모 클래스");
}
}
class B extends A{
public B(){
//super : 슈퍼클래스 (부모 클래스)를 가리키는
//레퍼런스 변수
super.make();
}
public void make(){//메소드 오버라이딩
System.out.println("자식 클래스");
}
}
public class Exten02 {
public static void main (String[] args){
B bp = new B();
bp.make();
//부모 값과 자식값 둘다 가져오게 된다
}
}
부모 클래스
| 예비 (0) | 2012.04.11 |
|---|---|
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 (0) | 2012.04.11 |
package com.over;//메서드 오버라이딩
class Parent{
protected void make(){
System.out.println("부모 클래스");
}
}
class Child extends Parent{
//메소드 오버라이딩(메소드 재정의) 메소드 오버라이딩 규칙 틀은 같으나 내용이 다름
//부모 클래스 메소드의 확장 또는 재정의
protected void make(){
System.out.println("자식 클래스");
}
}
public class Exten01 {
public static void main(String[] args){
Child ch = new Child();
//목적은 재사용 이 아니라 확장, 재정의(리모델링)
//메소드 오버라이딩으로 부모 클래스가 호출되지 않고 자식클래스로 바뀜
ch.make();
}
}
자식 클래스
package com.over2;//메서드 오버라이딩 기본2
class Parent{
String msg = "Parent 클래스";
public String getMsg(){
return msg;
}
}
class Child extends Parent{
String str = "Child 클래스";
//메소드 오버라이딩(재정의) -> 확장, 재정의(변형)
public String getMsg(){
return str;
}
}
public class Exten01 {
public static void main(String[] args){
Child ch = new Child();
System.out.println(ch.getMsg());
}
}
Child 클래스
package com.over;//메서드 오버라이딩
class Parent{
String msg = "겨울";
protected void make(){
System.out.println("부모 클래스");
}
}
class Child extends Parent{
//메소드 오버라이딩(메소드 재정의) 메소드 오버라이딩 규칙 틀은 같으나 내용이 다름
//부모 클래스 메소드의 확장 또는 재정의
//부모 클래스와 자식 클래스의 멤버변수명을 동일하게
//할경우는 자식클래스의 멤버변수가 호출됨
//단지 변수명이 같아서 자식 클래스 쪽 멤버변수가
//(호출되는 것이지 특별한 문법 사항이 있는것은 아님)
String msg = "봄";
//이건 오버라이딩 문법이 아님 메소드에만 있다
protected void make(){
System.out.println("자식 클래스");
}
}
public class Exten01 {
public static void main(String[] args){
Child ch = new Child();
//메소드 오버라이딩으로 부모 클래스가 호출되지 않고 자식클래스로 바뀜
//목적은 재사용 이 아니라 확장, 재정의(리모델링)
ch.make();
System.out.println(ch.msg);
}
}
자식 클래스
| 예비 (0) | 2012.04.11 |
|---|---|
| super 연습문제 (에러 찾기) (0) | 2012.04.11 |
| super로 은닉된 슈퍼클래스의 멤버변수 접근하기 (0) | 2012.04.11 |
| Super 기본 정수 (0) | 2012.04.11 |
| 메소드 오버라이딩 super 참조 변수 (0) | 2012.04.11 |