constructors and polymorphism

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85337565

Even though constructors are not polymorphic (they’re actually static methods, but the static declaration is implicit), it’s important to understand the way constructors work in complex hierarchies and with polymorphism.

// polymorphism/Sandwich.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Order of constructor calls
// {java polymorphism.Sandwich}
package polymorphism;

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();
  }
}
/* Output:
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
*/

This example shows the effects of composition, inheritance, and polymorphism on the order of construction.

The important class is Sandwich , which reflects three levels of inheritance (four, if you count the implicit inheritance from Object ) and three member objects. The output for creating a Sandwich object shows that the order of constructor calls for a complex object is as follows:

1. The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.

2. Member initializers are called in the order of declaration.

3. The body of the derived-class constructor is called.

Inside the constructor you must be certain all members are already built.

The main difference is polymorphism is a specific result of inheritancePolymorphism is where the method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method.

However, in a normal inheritance tree, you don't have to override any methods and therefore not all method calls have to be polymorphic. Does that make sense? It's a similar problem to all Ford vehicles are automobiles, but not all automobiles are Fords (although not quite....).

Additionally, polymorphism deals with method invocation whereas inheritance also describes data members, etc.

Is method overloading polymorphism?

Many programmers are confused about the relationship of polymorphism to method overriding and method overloading. In fact, only method overriding is true polymorphism. Overloading shares the same method’s name but the parameters are different. Polymorphism is a broad term, so there will always be discussions about this topic.

What’s the purpose of polymorphism?

The great advantage and purpose of using polymorphism is to decouple the client class from implementation code. Instead of being hard-coded, the client class receives the implementation to execute the necessary action.  In this way, the client class know just enough to execute its actions, which is an example of loose coupling.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/Sandwich.java

3. https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism

4. https://www.javaworld.com/article/3290403/core-java/java-challengers-3-polymorphism-and-inheritance.html

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85337565