Objective
Inheritance is all
OOPLanguage and
JavaIndispensable part of the language. When a class is created, it is always inherited, so unless explicitly stated to inherit from another class, it is implicitly derived from the
JavaThe standard root class
Objectto inherit.
The syntax of the composition is fairly plain, but inheritance uses a special syntax. In the inheritance process, you need to declare that the new class is similar to the old class. This declaration is done by writing the keyword immediately following the base class name before the left curly brace of the class body
extendsand realized. When you do this, all the fields and methods in the base class are automatically obtained. For example:
Sample source Code
Base class
Package Com.mufeng.theseventhchapter;public class Cleanser {private String s = "Cleanser";p ublic void Append (String a) {s + = A;} public void Dilute () {Append ("dilute ()");} public void Apply () {Append ("Apply ()");} public void Scrub () {Append ("scrub ()");} @Overridepublic String toString () {return s;} public static void Main (string[] args) {Cleanser x = new Cleanser (); X.dilute (); x.apply (); X.scrub (); SYSTEM.OUT.PRINTLN (x);}}
Sub-class
Package Com.mufeng.theseventhchapter;public class Detergent extends Cleanser {@Overridepublic void scrub () {Append (" Detergent.scrub () "); Super.scrub ();} public void Foam () {Append ("foam ()");} public static void Main (string[] args) {detergent x = new detergent (); X.dilute (); x.apply (); X.scrub (); X.foam (); SYSTEM.OUT.PRINTLN (x); SYSTEM.OUT.PRINTLN ("Testing base class:"); Cleanser.main (args);}}
Output results
Cleanser Dilute () apply () Detergent.scrub () scrub () foam () testing base class:cleanser Dilute () apply () scrub ()
SOURCE parsing
This program demonstrates the
JavaMany of the features. First, in
CleanserOf
Append ()method, we use the
"+ ="Operator adds several
Stringobjects are connected into
s, this operator is
JavaDesigner overloads are used to handle
StringOne of the operands of the object (the other is
"+")。 Secondly
CleanserAnd
detergentAll contain
Main ()Method. You can create one for each class
Main ()Method. This is set in each Class A
Main ()Method can make unit tests of each class simple and easy. And after the unit test is complete, there is no need to delete
Main ()method, you can leave it for the next test. Even if there are multiple classes in a program, only the main () method of the class called by the command line is called. So, in this case, if the command line is
Java detergentSo
Detergent.main ()will be called. Even
CleanserNot a
Publicclass if the command line
Java CleanserSo
Cleanser.main ()will still be called. Even if a class has only package access, its
Public Main ()is still accessible. In this example, you can see
Detergent.main ()Explicitly called the
Cleanser.main ()and passes the parameters obtained from the command line to it. Of course, it is also possible to pass arbitrary
StringArray.
CleanserAll of the methods must be
Public, which is very important. Keep in mind that if no access modifier is added, the member's default access is package access, which only allows members within the package to access it. Therefore, in this package, anyone can use these methods if they do not have access rights modifiers. For example
detergentis not a problem. However, one of the classes in the other package
Cleanser, you can only access the
PublicMembers. So, in order to inherit, the general rule is to designate all data members as
Private, all methods are specified as
Public。 Of course, in special cases, adjustments must be made, but the above method is indeed a very useful rule.
In
CleanserHas a set of methods in the interface:
Append ()、
Dilute ()、
apply ()、
Scrub ()And
toString ()。 Because
degergentis by keyword
extendsFrom
Cleanserexported, so it can automatically get these methods in its interface, although it is not possible to see these methods
detergentThe display definition in the. Therefore, inheritance can be treated as a reuse of classes as we
Scrub ()As seen, it is possible to use the methods defined in the base class and modify them. In this case, you might want to invoke the method inherited from the base class in the new version. But in
Scrub (), and cannot be called directly from the
Scrub (), because doing so will result in recursion, which is not what you expect. To resolve this issue,
JavaUse
SuperThe keyword denotes the meaning of the superclass, and the current class inherits from the superclass. To do this, the expression
Super.scrub ()Will call the base class version of the
Scrub ()Method. In the process of inheritance, you do not necessarily have to use the method of the base class. You can also add a new method to the export class, which is added in the same way that you add any method to the class, defining it. The foam () method is one example. Readers in
Detergent.main ()Will find that for a
detergentObject, in addition to calling the
detergentThe method (i.e.
foam ()), you can also call the
CleanserAll available methods in the.
Java code reuse mechanism reuse class inheritance syntax (with source)