(Method)Overriding
Modify method which is from parent.
Override : v.덮어쓰다
class Point {
int x;
int y;
String getLocation() {
return "x :" + x + ",y :"+y;
}
}
class Point3D extends Point {
int z;
//Method declaration does not change, but contents change.
String getLocation() {
return "x :" + x + ",y : "+y + ",z : "+z;
}
}class Point extends Object{
int x;
int y;
//Object클래스의 toString()을 오버라이딩.
public String getLocation() {//선언부는 똑같아야함!
return "x :" + x + ",y :"+y;
}
}
class Point3D extends Point {
int z;
//Method declaration does not change, but contents change.
String getLocation() {
return "x :" + x + ",y : "+y + ",z : "+z;
}
}Make code clear and simple using constructor and overriding
overriding condition
method declartion must be same as parent class. : String getLocation() => return type, method name, parameter list
access modifier <= parent class method access modifier. ex) public, protected, (dafult) private
exception <= parent class method exception.
overloading and overriding
Technicall those are different.
overloading : Define new method
overriding : Modify inherited method contents
Last updated
Was this helpful?