CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
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.
The syntax for composition is obvious, but to perform inheritance there's a distinctly different form. When you inherit, you say "This new class is like that old class." You state this in code by giving the name of the class as usual, but before the opening brace of the class body, put the keyword extends followed by the name of the base class. When you do this, you automatically get all the data members and methods in the base class. Here's an example:
//: Detergent.java
// Inheritance syntax & properties
class Cleanser
public void dilute()
public void apply()
public void scrub()
public void print()
public static void main(String[] args)
public class Detergent extends Cleanser
// Add methods to the interface:
public void foam()
// Test the new class:
public static void main(String[] args)
This demonstrates a number of features. First, in the Cleanser append( ) method, Strings are concatenated to s using the += operator, which is one of the operators (along with '+') that the Java designers "overloaded" to work with Strings.
Second, both Cleanser and Detergent contain a main( ) method. You can create a main( ) for each one of your classes, and it's often recommended to code this way so that your test code is wrapped in with the class. Even if you have a lot of classes in a program only the main( ) for the public class invoked on the command line will be called. (And you can have only one public class per file.) So in this case, when you say java Detergent, Detergent.main( ) will be called. But you can also say java Cleanser to invoke Cleanser.main( ), even though Cleanser is not a public class. This technique of putting a main( ) in each class allows easy unit testing for each class. And you don't need to remove the main( ) when you're finished testing; you can leave it in for later testing.
Here, you can see that Detergent.main( ) calls Cleanser.main( ) explicitly.
It's important that all of the methods in Cleanser are public. Remember that if you leave off any access specifier the member defaults to "friendly," which allows access only to package members. Thus, within this package, anyone could use those methods if there were no access specifier. Detergent would have no trouble, for example. However, if a class from some other package were to inherit Cleanser it could access only public members. So to plan for inheritance, as a general rule make all fields private and all methods public. (protected members also allow access by derived classes; you'll learn about this later.) Of course, in particular cases you must make adjustments, but this is a useful guideline.
Note that Cleanser has a set of methods in its interface: append( ), dilute( ), apply( ), scrub( ) and print( ). Because Detergent is derived from Cleanser (via the extends keyword) it automatically gets all these methods in its interface, even though you don't see them all explicitly defined in Detergent. You can think of inheritance, then, as reusing the interface. (The implementation comes along for free, but that part isn't the primary point.)
As seen in scrub( ), it's possible to take a method that's been defined in the base class and modify it. In this case, you might want to call the method from the base class inside the new version. But inside scrub( ) you cannot simply call scrub( ), since that would produce a recursive call, which isn't what you want. To solve this problem Java has the keyword super that refers to the "superclass" that the current class has been inherited from. Thus the expression super.scrub( ) calls the base-class version of the method scrub( ).
When inheriting you're not restricted to using the methods of the base class. You can also add new methods to the derived class exactly the way you put any method in a class: just define it. The extends keyword suggests that you are going to add new methods to the base-class interface, and the method foam( ) is an example of this.
In Detergent.main( ) you can see that for a Detergent object you can call all the methods that are available in Cleanser as well as in Detergent (i.e. foam( )).
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 638
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved