`
paddy.w
  • 浏览: 506138 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Overriding and Hiding Methods

    博客分类:
  • Java
阅读更多
        方法的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 MethodSuperclass Static Method
Subclass Instance MethodOverridesGenerates a compile-time error
Superclass Static MethodGenerates a compile-time errorHides

        简单说就是实例方法之间构成重写,类方法之间构成隐藏。实例方法和类方法之间会出现编译错误。

        官方文档链接:http://download.oracle.com/javase/tutorial/java/IandI/override.html
分享到:
评论

相关推荐

    Spring Boot: Bean definition overriding

    在本文中,我将讨论棘手的Spring Boot bean定义覆盖机制。 为了使您对该主题更加清楚,让我们从小测验开始。请看下一个简单的例子。 因此,我们有2种配置,它们使用名称beanName实例化bean,在主应用程序中,我们仅...

    Overloading&Overriding

    "Overloading&Overriding" 在面向对象编程中,Overloading和Overriding是两个重要的概念,它们都是多态性的体现。下面我们将对这两个概念进行详细的解释。 1. 方法重载(Overloading) 方法重载是让类以统一的...

    simpledraw

    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 ...

    Java教程补充材料

    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 ...

    java面试题英文版及其答案

    Public methods (getters and setters) are then provided to manipulate these private members, ensuring data integrity and controlling the interaction with the object's internal state. 9. What are the ...

    Mastering_Perl_Tk_1st_ed_2002.pdf

    General guidelines for configuring buttons are provided, including setting default options and overriding them for individual buttons. **4.21 Flashing the Button** Techniques for making a button ...

    1Z0-811 Exam Guide to Have a Cakewalk in Oracle Java SE Certific

    - **Overloading and Overriding:** Learn about method overloading and overriding. 3. **Decision Statements:** - **If-Else Statements:** Master the use of if-else statements for conditional logic. -...

    多型与虚拟Polymorphism in C++

    6. 覆盖与隐藏(Overriding and Hiding) 覆盖是指在派生类中重新定义基类的虚函数,保持相同的函数名、返回类型和参数列表。覆盖的目的是为了改变基类的行为。而隐藏是指派生类中定义了与基类同名的非虚函数,导致...

    Java The Complete Reference ,11th Edition.pdf

    Method overloading and overriding Inheritance Local variable type inference Interfaces and packages Exception handling Multithreaded programming Enumerations, autoboxing, and annotations The I/O ...

    Softgroup Components.NET Subclass and Hook Objects v2.0.3870

    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 ...

    PROFESSIONAL F# 2.0

    This chapter explains how to implement inheritance in F#, including overriding methods and accessing base class members. - **Generics (Chapter 10):** Generics provide a way to write type-safe code ...

    Django 1.0 Website Development.pdf

    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 ...

    Problem Solving with C++ (7th edition)

    - **Public and Private Members**: Discussion of access specifiers, particularly public and private, and their implications on data hiding and encapsulation. - **Separate Interface and Implementation**...

    Addison.Wesley.C.Plus.Plus.Coding.Standards.101.Rules.Guidelines.and.Best.Practices.Oct.2004.eBoo.chm

    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 ...

    Xtend User Guide

    其中方法部分又细分为抽象方法(Abstract Methods),覆盖方法(Overriding Methods),声明异常(Declared Exceptions),推断返回类型(Inferred Return Types),泛型方法(Generic Methods),操作符声明...

    java注释模板

    java 常用注释模板;包括 files、types、fields、methods、overriding methods等

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    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: ...

    Java入门与进阶设计第11章:继承与Overriding.ppt

    在Java入门与进阶设计第11章中,我们主要探讨了两个关键概念:继承(Inheritance)和覆盖(Overriding)。 首先,所有的Java对象都间接或直接地继承自`java.lang.Object`类,它是所有类的终极祖先。当你创建一个新...

Global site tag (gtag.js) - Google Analytics