OCA Java SE 7 Programmer I Study Guide (Exam 1Z0-803) (Oracle Press) by Liguori Robert & Finegan Edward

OCA Java SE 7 Programmer I Study Guide (Exam 1Z0-803) (Oracle Press) by Liguori Robert & Finegan Edward

Author:Liguori, Robert & Finegan, Edward [Liguori, Robert]
Language: eng
Format: mobi, epub
Tags: -
Publisher: McGraw-Hill
Published: 2012-09-17T16:00:00+00:00


The first implementation of these animals is to create a unique class for each one. Each of the preceding classes has no relationship to the other. It is easy to see that the classes are all very similar and there is duplicated code among them. In fact, all the methods are the same except the bark(), meow(), and neigh() methods. Although there is no explicit relationship defined in the code, it is easy to infer that all three classes are related.

The same example can be better implemented by using inheritance. In the next simple example, three of the four methods that need to be implemented are common to each different animal. A dog, cat, and horse all eat, rest, and move in similar fashion. This common functionality can be placed in a general Animal class that defines all the general methods and instance variables that make up an animal. When the developer creates more specific types of animals such as dogs, cats, or horses, he or she can use the Animal class as a base, or superclass. The more specific classes will inherit all of the nonprivate methods and instance variables from the base Animal class. A class is inherited when it is extended. It is important to remember that a class can extend only one class. It is invalid to inherit multiple classes in one class. However, a class can inherit a class that then inherits another class, and so on. The extends keyword is used in the class signature line. The following is an example of the same animals being implemented using inheritance:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.