重载(overload)、重写(overwrite)、覆盖(override)- -

重载(overload)、重写(overwrite)、覆盖(override)- -

                                      


重载(overload)、重写(overwrite)、覆盖(override)
后两者的概念是相同的。前者是同一个类中,由于参数列表不同而方法名字相同。而后二者是子类中出现与父类完全一样的方法,从而覆盖了父类的方法。
class overWriteSuper
{
public void sayHello()
{
System.out.println("I'm in super class.");
}
}
class overWriteSub extends overWriteSuper {

public void sayHello()
{
System.out.println("I'm in sub class.");
}

public void sayGoodBye()
{
System.out.println("Good-bye all.");
}

public void sayGoodBye(String str)
{
System.out.println("Good-bye "+str+".");
}
}

public class overTest
{
public static void main(String[] args) {
overWriteSuper mySuper = new overWriteSuper();
mySuper.sayHello();
overWriteSub mySub = new overWriteSub();
mySub.sayHello();
overWriteSuper mySubOverWrite = new overWriteSub();//即使类型是父类但是还是调用子类的方法
mySubOverWrite.sayHello();
}
}

显示结果:
I'm in super class.
I'm in sub class.
I'm in sub class.

你可能感兴趣的:(重载(overload)、重写(overwrite)、覆盖(override)- -)