Modify method which is from parent.
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;
}
}
public class OverridngTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyPoint3 p = new MyPoint3(3, 5);//constructor
System.out.println(p);//overriding toString method
// p.x = 3;
// p.y = 5;
// System.out.println(p.toString());
// System.out.println(p.x);
// System.out.println(p.y);
}
}
Technicall those are different.
class Parent {
void parentMethod() {}
}
class Child extends Parent {
void parentMethod() {}//inherit method. overriding
void parentMethod(int i) {}//overloading
void childMethod() {}
void childMethod(int i) {}//overloading
}