方法的override体现在实例方法(instance method),hide体现在静态方法(class method)。
满足下面几个条件,则构成了类继承关系间方法的override:
1、相同方法名
2、相同返回值
3、相同参数,包括个数相同、类型相同
可以将以上三个条件一起称为相同声明。(
其实方法签名包括方法名+参数,但构成override还要包括返回值。编译器检查方法签名相同的时候会认定是同一个方法,强制返回值相同,如果override时返回值不同,编译器会报错)
注意:方法的访问修饰符对方法的override没有本质的影响。唯一需要注意的规范是:重写方法的访问权限必须大于被重写方法。
对于继承类之间的两个具有相同声明的static方法,则构成了方法之间的hide。假设new一个子类型的对象,并声明为父类型。那么调用方法(父子之间构成hide的方法)时会调用父类型中的方法。也就是总是调用声明类中的方法(前提必须是父子之间的方法构成hide关系)。下面是java官方文档中的描述:
The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.
举一个例子:
public class StaticTest {
public static void main(String[] args) {
Test t=new Test2();
t.print();
}
}
class Test{
protected static void print(){
System.out.println("from Test");
}
}
class Test2 extends Test{
public static void print(){
System.out.println("from Test2");
}
}
结果输出:from Test
官方文档中有这么一个表格,清楚的看到父子类之间有相同声明的方法是构成重写、隐藏还是编译错误。
| Subclass Instance Method | Superclass Static Method |
Subclass Instance Method | Overrides | Generates a compile-time error |
Superclass Static Method | Generates a compile-time error | Hides |
简单说就是实例方法之间构成重写,类方法之间构成隐藏。实例方法和类方法之间会出现编译错误。
官方文档链接:
http://download.oracle.com/javase/tutorial/java/IandI/override.html
分享到:
相关推荐
Overloading&Overriding两者的区别
In addition, by sub classing a class and overriding one or two methods, you can provide your own desired nodes. The line (link) object of the control is very flexible, and can be hook to other ...
7.9 Renaming and Hiding Members 86 7.10 Implicit Imports 86 Exercises 87 ix Contents8 INHERITANCE A1 91 8.1 Extending a Class 91 8.2 Overriding Methods 92 8.3 Type Checks and Casts 93 8.4 Protected ...
11 Hiding Data Fields and Static Methods 12 Initialization Blocks 13 Extended Discussions on Overriding Methods 14 Design Patterns 15 Text I/O Prior to JDK 1.5 (Reader and Writer Classes) 16 ...
Overriding Methods 169 Which Method Is Selected? 171 Abstract Classes 173 Exercises 174 9 Polymorphism, Dynamic Typing, and Dynamic Binding 177 Polymorphism: Same Name, Different Class 177 Dynamic ...
The Form of a Subclass Definition 117 Overriding Superclass Methods 119 Instance Variables 119 Entry and Exit Points 121 Initialize or Decode? 122 Storing and Accessing Properties 123 Key-Value ...
Method Overloading and OverRiding
SCJP6 考试题 及其答案,试题包括IO,Generics,Overriding and Overriding,Threads and Java6的新特性。
Method overloading and overriding Inheritance Local variable type inference Interfaces and packages Exception handling Multithreaded programming Enumerations, autoboxing, and annotations The I/O ...
However, to wrap subclassing and hooking into virtual function overriding it is an obscure technique unknown to a large population of Windows programmers. This is unfortunate, because there are many ...
Java入门与进阶设计第11章:继承与Overriding.ppt
Fields and methods ..................... 47 Methods, arguments, and return values ................. 48 The argument list ......................... 49 Building a Java program ...... 50 Name visibility ...
Hiding and showing elements 101 Accessing CSS properties and HTML attributes 102 Manipulating HTML documents 103 Traversing the document tree 103 Handling events 104 Sending AJAX requests 105 ...
How do you practice "safe" overriding? When should you provide a no-fail swap? Why and how should you prevent exceptions from propagating across module boundaries? Why shouldn't you write namespace ...
CSS for Windows 8 App Development is your learning guide for CSS – the language of great Windows 8-style apps....Overriding and Defining Styles CSS Libraries and Resources Styling SVG
在本文中,我将讨论棘手的Spring Boot bean定义覆盖机制。 为了使您对该主题更加清楚,让我们从小测验开始。请看下一个简单的例子。 因此,我们有2种配置,它们使用名称beanName实例化bean,在主应用程序中,我们仅...
java 常用注释模板;包括 files、types、fields、methods、overriding methods等
Item 10: Obey the general contract when overriding equals Item 11: Always override hashCode when you override equals Item 12: Always override toString Item 13: Override clone judiciously Item 14: ...
This book uses Java and Eclipse to write and generate output for examples in topics such as classes, interfaces, overloading, and overriding. Interactive Object Oriented Programming in Java uniquely ...
$ npm install hapi-overriding 用法 hapi-overriding通过扩展请求生命周期的onRequest步骤来工作。 对于任何传入的 POST 请求,扩展函数检查路径是否以定义的操作名称之一(例如destroy )结尾。 如果是这样,它会...