可以,父类有的方法子类也有,所有完全可以重载
public class Son extends Father{
public void test(int i){
System.out.println("This is from Son class and the number is " +i);
}
public static void main(String args[]){
Son son = new Son();
son.test();
son.test(1);
}
}
class Father{
public void test(){
System.out.println("This is from Father class");
}
}
输出结果为This is from Father class
This is from Son class and the number is 1
public class Son extends Father{
public void test(int i){
System.out.println("This is from Son class and the number is " +i);
}
public static void main(String args[]){
Son son = new Son();
son.test();
son.test(1);
}
}
class Father{
public void test(){
System.out.println("This is from Father class");
}
}
输出结果为This is from Father class
This is from Son class and the number is 1
public Class Test{
public void test(){
}
public void test(int a){
}
public void test(int a,int b){
}
}
这样,具有相同的方法名,参数的类型不同,或者参数的个数不同,就是重载(但是不能根据函数的返回值确定是否重载)
而覆盖是发生在子类和父类中的:
public abstract Class Animal{
public abstract void cry();
}
public Class Cat extends Animal{
public void cry(){
System.out.println("Cat is cry...");
}
}
这样就实现了覆盖。。
A) 重载是在子类中实现
B) 重载必须参数列表相同
C) 重载必须返回类型不同
D) 重载的实现必须是在同一个作用域中
为什么答案选D
public class OverloadTest {
public void fun(){
System.out.println("method fun in OverloadTest,no parameter");
}
public static void main(String[] args){
OverloadTest1 test1=new OverloadTest1();//这里定义了OverloadTest1的实例,
//因为OverloadTest1是OverloadTest的子类,所以test1会调用OverloadTest中的方法。
try {
int i=1,j=2,m=3;
test1.fun();
test1.fun(i,j,m);
} catch (Exception e) {
}
}
}
class OverloadTest1 extends OverloadTest{
public void fun(int i,int m,int n){
System.out.println("Overload fun in OverloadTest1,parameter type:int,int,int");
}
}
}