Java Tour (9) polymorphism

Source: Internet
Author: User
Tags abstract constructor contains final inheritance interface
Polymorphism is the third feature of object-oriented language after data abstraction and inheritance.



Binding (binding) (looks like a transliteration word): Connecting a method's call to the method itself is called a binding, when the binding occurs before the program is run, is called a earlybinding, and when the program is run, the binding method is determined to be bound by the type of the object, It is also called run-time binding (Run-time binding) or dynamic binding, and all Java methods are back-bound, which means that, in general,

You don't have to consider whether to adopt the post binding, all of which are automatic.



There is a classic example of "shape" that can be vividly illustrated by what is post binding.




In this example, the base class is the shape class, which has several derived classes: Circle, Square, Triangle,

Shape s = new Circle ();

This creates a circle object and then gives it a shape, which seems a bit inappropriate, but it's good because circle is really a shape, and then suppose you call a method of a base class

S.draw ();
You might think that this call should be the draw method of shape, but not that it calls the Circle Draw (), which is the reason for the subsequent binding of the implementation. The concrete implementation is that the base class defines a common interface-that is, all the shape has the draw () method and the Erase () method, and the derived class will overwrite the two methods to provide a different behavior. Here I wonder why not write directly:

Circle s = new Circle ();

S.draw ();

Later, I saw the author change This example randomly to create a circle, Square, triangle object, because it is not yet known what the object is created, so only by using dynamic binding as in the previous notation.



Thus we see the greatest advantage of polymorphism: scalability. We can add as few new types as we need without worrying about modifying the methods in the base class, so in a well-designed OOP program, most of the methods will be just like the draw () method, dealing with only the base class interface. This program is extensible because you can add new functionality by "Getting new data types to inherit the common base class" approach. The methods that deal with the base class interface do not need to be modified to accommodate the new class.



Polymorphism is a very important technology for programmers, and it allows you to "separate and not change".



Abstract classes and abstract methods: it makes no sense to create class objects like the shape class, not to mention that you might want to prevent users from doing so, so we can use abstract methods to solve the problem. Shaped like:

abstract void f ();
A class that contains one or more abstract methods is an abstract class (which contains an abstract method that must be defined as a bit abstract class), and the function of an abstract class is to manipulate a set of classes through a common interface. Its method is like the method of the base class in the example above, just like the goods. And if you create an object that is an abstract class, the compiler will make an error.



If you inherit an abstract class and intend to create an object of that class, you must implement all the methods defined by the base class, or else an abstract method exists, and the class is an abstract class.



It is possible to create an abstract class that does not contain abstract methods, which can be used in situations where "you do not have to create abstract methods but want to prohibit others from creating objects of this class."



Constructors are always different, and polymorphism is no exception. First, consider an example and review the order in which constructors are called first.

///////////////////////////////////////////////////////////////////////////////////

Class Meal {
Meal () {System.out.println ("Meal ()");}
}
Class Bread {
Bread () {System.out.println ("Bread ()");}
}
Class Cheese {
Cheese () {System.out.println ("Cheese ()");}
}
Class Lettuce {
Lettuce () {System.out.println ("lettuce ()");}
}
Class Lunch extends Meal {
Lunch () {System.out.println ("Lunch ()");}
}
Class Portablelunch extends Lunch {
Portablelunch () {System.out.println ("portablelunch ()");}
}

public class Sandwich extends Portablelunch {
Private Bread B = new Bread ();
Private Cheese C = new Cheese ();
Private lettuce L = new lettuce ();
Public Sandwich () {
System.out.println ("Sandwich ()");
}
public static void Main (string[] args) {
New Sandwich ();
SYSTEM.OUT.PRINTLN ("Correct output:");
System.out.println (
"Meal () \ n" +
"Lunch () \ n" +
"Portablelunch () \ n" +
"Bread () \ n" +
"Cheese () \ n" +
"Lettuce () \ n" +
"Sandwich ()"
);
}
}
///////////////////////////////////////////////////////////////////////////

That is to say, the order in which the constructors of complex objects are called:

1, call the constructor of the base class. This is a recursive process, so you first create the root of the inheritance system, then the next-level derived class, and so on until the last inherited class constructor.

2, the member objects are initialized according to the order of the objects they declare.

3, executes the body of the constructor of the inheriting class.



Clean work, although not commonly used, but a very need for careful work.



A good constructor should be, "set the state of the object with the least amount of effort, and avoid calling the method as much as possible" the only method that the constructor can safely invoke is the final method of the base class. (This article also applies to private because it is final automatically) they do not overwrite and therefore do not produce such unexpected behavior.



Cond......







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.