java基础之继承和多态概念
Inheritance
Inheritance is a reusability mechanism in object-oriented programming. With inheritance, the commonproperties of various objects are exploited to form relationships with each other. The abstract and common
properties are provided in the superclass, which is available to the more specialized subclasses. For example,
a color printer and a black-and-white printer are kinds of a printer (single inheritance); an all-in-one printer
is a printer, scanner, and photocopier (multiple inheritance).
Why is inheritance a powerful feature? Because it supports modeling classes in a hierarchy, and such
hierarchical models are easy to understand. For example, you can logically categorize vehicles as two-wheelers, three-wheelers, four-wheelers, and so on. In the four-wheelers category, there are cars, vans,
buses, and trucks. In the cars category, there are hatchbacks, sedans, and SUVs. When you categorize
hierarchically, it becomes easy to understand, model, and write programs.
Polymorphism
The Greek roots of the term polymorphism refer to the “several forms” of an entity. In the real world,every message you communicate has a context. Depending on the context, the meaning of the message may
change and so may the response to the message. Similarly in OOP, a message can be interpreted in multiple
ways (polymorphism), depending on the object.
• When different forms of a single entity are resolved duringruntime (late binding),
such polymorphism is called dynamic polymorphism. In the previous section
on inheritance, we discussed overriding. Overriding is an example of runtime
polymorphism.
such polymorphism is called static polymorphism. Function overloading is an
example of static polymorphism.
Runtime Polymorphism
You just learned that a base class reference can refer to a derived class object. You can invoke methods fromthe base class reference; however, the actual method invocation depends on the dynamic type of the object
pointed to by the base class reference. The type of the base class reference is known as the static type of the
object and the actual object pointed by the reference at runtime is known as the dynamic type of the object.
When the compiler sees the invocation of a method from a base class reference and if the method
is an overridable method (a nonstatic and nonfinal method), the compiler defers determining the exact
method to be called to runtime (late binding). At runtime, based on the actual dynamic type of the object, an
appropriate method is invoked. This mechanism is known as dynamic method resolution or dynamic method
invocation.
Now, let’s ask a more fundamental question: Why do you need to override methods? In OOP, the
fundamental idea in inheritance is to provide a default or common functionality in the base class; the
derived classes are expected to provide more specific functionality.
Method Overloading
In a class, how many methods can you define with the same name? Many! In Java, you can define multiplemethods with the same name, provided the argument lists differ from each other. In other words, if you
provide different types of arguments, different numbers of arguments, or both, then you can define multiple
methods with the same name. This feature is called method overloading. The compiler will resolve the call to
a correct method depending on the actual number and/or types of the passed parameters.
Such overloaded methods are useful for avoiding repeating the same code in different functions.
Constructor Overloading
A default constructor is useful for creating objects with a default initialization value. When you want toinitialize the objects with different values in different instantiations, you can pass them as the arguments to
constructors. And yes, you can have multiple constructors in a class, which is constructor overloading. In a
class, the default constructor can initialize the object with default initial values, while another constructor
can accept arguments that need to be used for object instantiation. You can
used the this keyword (which refers to the current object) to call one constructor from another constructor
of the same class.
Points to Remember
Here are some interesting rules regarding method overloading :
• Overload resolution takes place entirely at compile time (not at runtime).
• You cannot overload methods with the methods differing in return types alone.
• You cannot overload methods with the methods differing in exception specifications alone.
• For overload resolution to succeed, you need to define methods such that the
compiler finds one exact match. If the compiler finds no matches for your call or
if the matching is ambiguous, the overload resolution fails and the compiler issues
an error.
The signature of a method is made up of the method name, number of arguments, and types of
arguments. You can overload methods with same name but with different signatures. since return type and
exception specification are not part of the signature, you cannot overload methods based on return type or
exception specification alone.
Invoking Superclass Methods
It is often useful to call the base class method inside the overridden method. To do that, you can use thesuper keyword. In derived class constructors, you can call the base class constructor using the super
keyword. Such a call should be the first statement in a constructor if it is used. You can use the super
keyword for referring to the base class members also. In those cases, it need not be the first statement in the
method body.
if you’re using an object in containers like HashSet or HashMap, make sure you override the hashCode()
and equals() methods correctly. if you don’t, you’ll get nasty surprises (bugs) while using these containers!
Okay, how do you override the hashCode() method? In the ideal case, the hashCode() method should
return unique hash codes for different objects.
The hashCode() method should return the same hash value if the equals() method returns true. What
if the objects are different (so that the equals() method returns false)? It is better (although not required) for
the hashCode() to return different values if the objects are different. The reason is that it is difficult to write a
hashCode() method that gives unique value for every different object.
The methods hashCode() and equals() need to be consistent for a class. For practical purposes, ensure
that you follow this one rule: the hashCode() method should return the same hash value for two objects if the
equals() method returns true for them.
读书笔记:
Oracle Certified Professional Java SE 8 Programmer Exam 1Z0-809: A Comprehensive
OCPJP 8 Certification Guide
Copyright © 2016 by S G Ganesh, Hari Kiran, and Tushar Sharma
ISBN-13 (pbk): 978-1-4842-1835-8
ISBN-13 (electronic): 978-1-4842-1836-5