JHTP小结_第十章_面向对象编程-多态(Polymorphism)
来源:互联网 发布:js 弹出对话框 编辑:程序博客网 时间:2024/06/05 18:53
Section10.1 Introduction
• Polymorphism (p. 396)enables us to write programs that process objects that share the same superclass as if they were all objects of the superclass; this can simplify programming.
• With polymorphism, we can design and implement systems that are easily extensible. The only parts of a program that must be altered to accommodate new classes are those that require direct knowledge of the new classes that you add to the hierarchy.
Section10.3 Demonstrating Polymorphic Behavior
• When the compiler encounters a method call made through a variable, it determines if the method can be called by checking the variable’s class type. If that class contains the proper method declaration (or inherits one), the call is compiled. At execution time, the type of the object to which the variable refers determines the actual method to use.
Section10.4 Abstract Classes and Methods
• Abstract classes (p.401) cannot be used to instantiate objects, because they’re incomplete.
• The primary purpose of an abstract class is to provide an appropriate superclass from which other classes can inherit and thus share a common design.
• Classes that can be used to instantiate objects are called concrete classes (p. 402). They provide implementations of every method they declare (some of the implementations can be inherited).
• Programmers often write client code that uses only abstract super classes (p. 402) to reduce client code’s dependencies on specific subclass types.
• Abstract classes sometimes constitute several levels of a hierarchy.
• An abstract class normally contains one or more abstract methods (p. 402).
• Abstract methods do not provide implementations.
• A class that contains any abstract methods must be declared as anabstractclass (p. 402). Each concrete subclass must provide implementations of each of the superclass’s abstract methods.
• Constructors andstaticmethods cannot be declaredabstract.
• Abstract superclass variables can hold references to objects of any concrete class derived from the superclass. Programs typically use such variables to manipulate subclass objects polymorphically.
• Polymorphism is particularly effective for implementing layered software systems.
Section10.5 Case Study: Payroll System Using Polymorphism
• A hierarchy designer can demand that each concrete subclass provide an appropriate method implementation by including anabstractmethod in a superclass.
• Most method calls are resolved at execution time, based on the type of the object being manipulated. This process is known as dynamic binding (p. 417) or late binding.
• A superclass variable can be used to invoke only methods declared in the superclass.
• Operatorinstanceof(p. 417) determines if an object has theis-arelationship with a specific type.
• Every object in Javaknows its own class and can access it throughObjectmethodgetClass(p. 418), which returns an object of typeClass(packagejava.lang).
• Theis-arelationship applies only between the subclass and its superclasses, not vice versa.
Section10.7finalMethods and Classes
• A method that’s declaredfinal(p. 419) in a superclass cannot be overridden in a subclass.
• Methods declaredprivateare implicitlyfinal, because you can’t override them in a subclass.
• Methods that are declaredstaticare implicitlyfinal.
• Afinalmethod’s declaration can never change, so all subclasses use the same implementation, and calls tofinalmethods are resolved at compile time—this is known as static binding (p. 420).
• The compiler can optimize programs by removing calls tofinalmethods and inlining their expandedcode at each method-call location.
• A class that’s declaredfinalcannot be extended (p. 420).
• All methods in afinalclass are implicitlyfinal.
Section10.9 Creating and Using Interfaces
• An interface (p. 421)specifieswhatoperations are allowed but nothowthey’re performed.
• A Java interface describes a set of methods that can be called on an object.
• An interface declaration begins with the keywordinterface(p. 421).
• All interface members must bepublic, and interfaces may not specify any implementation details, such as concrete method declarations and instance variables.
• All methods declared in an interface are implicitlypublic abstractmethods and all fields are implicitlypublic,staticandfinal.
• To use an interface, a concrete class must specify that itimplements(p. 421) the interface and must declare each interface method with the signature specified in the interface declaration. A class that does not implement all the interface’s methods must be declared abstract.
• Implementing an interface is like signing a contract with the compiler that states, “I will declare
all the methods specified by the interface or I will declare my classabstract.”
• An interface is typically used when disparate (i.e., unrelated) classes need to share common methods and constants. This allows objects of unrelated classes to be processed polymorphically—objects of classes that implement the same interface can respond to the same method calls.
• You can create an interface that describes the desired functionality, then implement the interface in any classes that require that functionality.
• An interface is often used in place of anabstractclass when there’s no default implementation to inherit—that is, no instance variables and no default method implementations.
• Likepublic abstractclasses, interfaces are typicallypublictypes, so they’re normally declared in files by themselves with the same name as the interface and the.javafilename extension.
• Java does not allow subclasses to inherit from more than one superclass, but it does allow a class to inherit from a superclass and implement more than one interface.
• All objects of a class that implement multiple interfaces have theis-arelationship with each implemented interface type.
• An interface can declare constants. The constants are implicitlypublic,staticandfinal.
Section10.10 Java SE 8 Interface Enhancements
• In Java SE 8, an interface may declaredefaultmethods—that is,publicmethods with concrete implementations that specify how an operation should be performed.
• When a class implements an interface, the class receives the interface’sdefaultconcrete implementation if it does not override them.
• To declare adefaultmethod in an interface, you simply place the keyworddefaultbefore the method’s return type and provide a complete method body.
• When you enhance an existing an interface withdefaultmethods—any classes that implemented the original interface will not break—it’ll simply receive the default method implementations.
• With default methods, you can declare common method implementations in interfaces (rather
thanabstractclasses), which gives you more flexibility in designing your classes.
• As of Java SE 8,interfaces may now includepublic staticmethods.
• As of Java SE 8, any interface containing only one method is known as a functional interface.
There are many such interfaces throughout the Java APIs.
• Functional interfaces are used extensively with Java SE 8’s new lambda capabilities. As you’ll see,lambdas provide a shorthand notation for creating anonymous methods.
- JHTP小结_第十章_面向对象编程-多态(Polymorphism)
- JHTP自测题_第十章_面向对象编程-多态(Polymorphism)
- JHTP小结_第九章_面向对象编程-继承(Inheritance)
- JHTP自测题_第九章_面向对象编程-继承(Inheritance)
- JHTP小结_第十五章_文件、流和对象序列化(Files, Streams, and Object Serialization)
- JHTP小结_第三章_类、对象、方法及字符串简介
- JHTP小结_第八章_深入理解类和对象
- JHTP小结_第十三章_图形及Java2D
- JHTP小结_第六章_深入理解Methods(方法)
- JHTP小结_第七章_数组及动态数组(Array and ArrayList)
- JHTP小结_第十一章_深入理解异常(Exception Handling)
- JHTP小结_第十二章_GUI组件(上篇)-Part 1
- JHTP小结_第十二章_GUI组件(上篇)-Part 2
- JHTP小结_第十四章_字符串、字符及正则表达式(Strings, Characters and Regular Expressions)
- JHTP小结_第四章_控制语句Part 1-赋值、++、--运算符
- JHTP小结_第五章_控制语句Part 2-逻辑运算符
- JHTP自测题_第十五章_文件、流和对象序列化(Files, Streams, and Object Serialization)
- JHTP自测题_第十二章_GUI组件(上篇)
- Alamofire 上传图片入门教程(上)
- ajax文件和文件其他属性进行上传给后台spring mvc
- “‘javac’不是内部或外部命令,也不是可运行的程序或批处理文件”解决过程
- getElementsByClassName兼容性处理
- JAVA之Swing设计
- JHTP小结_第十章_面向对象编程-多态(Polymorphism)
- 4.docker学习笔记:创建自己的registry
- 文件搜索器的实现(二)
- 在linux中添加ftp用户,并设置相应的权限
- WINCE互斥的实现
- scrollTop兼容性处理
- 设计模式——抽象工厂
- 深入理解React中的上下文this
- hdu5411 CRB and Puzzle(矩阵)