home / experts / javascript / column80 |
|
Protecting Private DataWe saw on the previous page that private data members are not protected. Each subclass can change them permanently, affecting the values seen by all other subclasses. One way to overcome this problem is to call the superclass constructor from within each subclass constructor. In this way, each subclass creates a local copy of the private data members, and does not step over its peer subclasses memory. There are two ways to call a constructor from within a constructor. One way is to use the
After we define Try it. The second way to call a constructor from within a constructor is by defining the superclass constructor as a method of the subclass constructor. Here is the superclass Shape() and the subclass SquareA():
After we define Try it. Next: A Final Word |
URL: http://www.webreference.com/js/column80/9.html