One of the most compelling features about Java is code reuse . But to be revolutionary, you’ve got to be able to do a lot more than copy code and change it.
Latest Thinking In Java Articles - Page 5
It is very common to use composition and inheritance together. The following example shows the creation of a more complex class, using both inheritance and composition, along with the necessary constructor initialization:
Inheritance is such an integral part of Java (and OOP languages in general) that it was introduced in Chapter 1 and has been used occasionally in chapters before this one because certain situations required it. In addition, you’re always doing inheritance when you create a class, because if you don’t say otherwise you inherit from Java’s standard root class Object .
Now that you’ve been introduced to inheritance, the keyword protected finally has meaning. In an ideal world, private members would always be hard-and-fast private , but in real projects there are times when you want to make something hidden from the world at large and yet allow access for members of derived classes. The protected keyword is a nod to pragmatism. It says “This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package .” That is, protected in Java is automatically “friendly.”
The most important aspect of inheritance is not that it provides methods for the new class. It’s the relationship expressed between the new class and the base class. This relationship can be summarized by saying “The new class is a type of the existing class.”
Latest CodeGuru Developer Columns
Here, the proverbial rubber meets the road. Finish this project and create a working magnifier.
Create your own mouse maze game. Examples are both in C# and VB.NET.
Discover the world of Portable Class Libraries (PCLs) and how they can work for you.