Test3中protection() method override 情况在 interface 和 superclass中怎么不一样
(请看下面例子)//for interface
interface Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
}class Test2 implements Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}public class Test3 extends Test2
{ public static void main(String[] args)
{ (new Test2()).protection(3); //compilation error:modifier protected not allowed it overrides the method in Test1
}
}//for superclass
interface Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
}class Test2 implements Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}public class Test3 extends Test2
{ public static void main(String[] args)
{ (new Test2()).protection(3); //no compilation error which means that it overrides the method in Test2 which is different from the situation above
}
}这是什么原理啊
先谢谢了
(请看下面例子)//for interface
interface Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
}class Test2 implements Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}public class Test3 extends Test2
{ public static void main(String[] args)
{ (new Test2()).protection(3); //compilation error:modifier protected not allowed it overrides the method in Test1
}
}//for superclass
interface Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
}class Test2 implements Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}public class Test3 extends Test2
{ public static void main(String[] args)
{ (new Test2()).protection(3); //no compilation error which means that it overrides the method in Test2 which is different from the situation above
}
}这是什么原理啊
先谢谢了
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
} 接口可以有方法体?应这样吧:
interface Test1
{ protected void protection(int x);
}
ide用惯了
查错误水平次了
对 已经改正~~~ 谢谢为什么两个(interface 和 superclass)会不同啊?
interface 中文应该是“接口”接口支持多重继承,而基类仅支持单继承。楼主的例子很简单:接口带有方法体编译错误;基类带有方法体编译失败。
原因很简单:接口的作用就是用于被继承的,即给其他类或接口提供接口的,所以他不需要方法体,另外接口可以用于多继承,这就需要他简单易用。而基类则不同,基类首先是类,既然是类就需要有自己的属性和行为(方法),而基类的继承相当于是扩展-----提供新的方法或覆盖原有方法增加新的内容。
不好意思 貌似 superclass粘贴错了~~
更正如下
//for superclass
class Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
} class Test2 implements Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
} public class Test3 extends Test2
{ public static void main(String[] args)
{ (new Test2()).protection(3); //no compilation error which means that it overrides the method in Test2 which is different from the situation above
}
} 您的意思是 superclass有自己的属性和行为
为什么 都是Test3 extends Test2 protection() method 没有都override Test2中的 method?
(第一个 编译结果是 Test3中的method错误地override the protected method in the interface.)
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
} 改为:class Test2 extends Test1
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}
我的意思是 对于interface Test2 implements Test1 Test3 extends Test2 根据编译结果 protection() method 是 override Test1(this is an interface here) 中的protection method 而没有直接override Test2中的method
但是 对于superclass Test2 extends Test1 Test3 extends Test2 根据编译结果 protection() method 是直接 override Test2(this is a superclass of Test2 here) 中的protection method 而没有override Test1(也是个superclass)中的method为什么会这样呢?
你是怎么看出来Test3中的protection()方法是override 接口Test1的?interface中的的方法是没有方法体的;
对于extended
Test2已经继承了Test1,并且override 了protection()方法;Test3继承Test2,
Test3中还是new Test2(),当然是Test2的protection()方法,即父类方法.
//for superclass
class Test1
{ protected void protection(int x)
{ System.out.println("1!!!"+x);
}
} class Test2 implements Test1 //明显有问题,test1也不是借口你怎么能实现,应该是继承啊
{ public void protection(int x)
{ System.out.println("2!!!"+x);
}
}
但是如果实子类继承父类,那么只有父类中的private方法不能被重载,其他的可以被重载(重载即参数个数可以不同,返回值类型和方法名必须相同)
首先,我想说,你的源代码有很多基础性错误(尤其是接口知识),根本不能通过编译.
其次,我修改了你的代码(剔除错误),对照你的源代码再看看://for interface
interface Test1 {
void protection(int x);//接口方法默认是public的,不能是protected,并且没有方法体
}
class Test2 implements Test1 { @Override
public void protection(int x) {
System.out.println("2!!!" + x); }
}public class Test3 extends Test2 {
public static void main(String[] args) {
(new Test2()).protection(3); // compilation error:modifier protected not
// allowed it overrides the method in
// Test1
}
}// for superclass
class Test1 {
protected void protection(int x) {
System.out.println("1!!!" + x);
}
}class Test2 extends Test1 {//此处是extends 不是implements
public void protection(int x) {
System.out.println("2!!!" + x);
}
}public class Test3 extends Test2 {
public static void main(String[] args) {
(new Test2()).protection(3);
// no compilation error which means that it
// overrides the method in Test2 which
// is different from the situation above
}
}结果都是继承Test2的方法
否则你根本无法针对你想要学习的知识写出正确的代码。你下面的代码也是完全混乱的。
先记住第一点,interface的method必须是public abstract。