Syntax for Java class inheritance

Source: Internet
Author: User
Tags command line inheritance

Inheritance is very tightly combined with Java (and other OOP languages). We have introduced the concept of inheritance in the 1th chapter, and have been used from time to moment in the chapters before this chapter, as some special occasions require inheritance. In addition, the creation of a class is definitely inherited, because otherwise it would inherit from Java's standard root class object. The
syntax for compositing is very simple and intuitive. But in order to inherit, a completely different form must be adopted. When we need to inherit, we say, "This new class is about the same as the old one." "For the concept of surface in code, the class name needs to be given." But before the beginning curly braces of the class body, you need to place a keyword extends followed by the name of the "base class". By doing this, you automatically get all the data members and methods of the underlying class. The following is an example:

 

: Detergent.java//Inheritance Syntax & Properties Class Cleanser {private String s = new String ("Cleanser");
  public void Append (String a) {s + = A;}
  public void Dilute () {Append ("dilute ()");}
  public void Apply () {Append ("Apply ()");}
  public void Scrub () {Append ("scrub ()");}
  public void print () {System.out.println (s);}
    public static void Main (string[] args) {Cleanser x = new Cleanser (); X.dilute (); X.apply ();
    X.scrub ();
  X.print (); } public class Detergent extends Cleanser {//change a method:public void scrub () {Append ("Detergent.scrub" (
    )"); Super.scrub ();
  Call Base-class Version}//ADD methods to the Interface:public void foam () {Append ("foam ()");}
    Test the new class:public static void Main (string[] args) {detergent x = new detergent ();
    X.dilute ();
    X.apply ();
    X.scrub ();
    X.foam ();
    X.print ();
    SYSTEM.OUT.PRINTLN ("Testing base class:");
  Cleanser.main (args); }
} ///:~ 

This example shows you a lot of features. First, in the cleanser append () method, the string is connected with an S. This is accomplished with the "+ =" operator. As with "+", "+ +" is used by Java to "overload" the string processing.
Second, both cleanser and detergent contain a main () method. We can create a main () for each of our classes. It is often recommended that you write code like this so that your own test code can be encapsulated into a class. Even though there are a large number of classes in the program, only main () is invoked for the public class that is requested on the command line. So in this case, when we use the "Java detergent", The call is Degergent.main ()-Even if cleanser is not a public class. This method of placing main () into each class makes it easy to unit test each class. And after the test is completed, no need to delete main (), you can keep it for future testing.
Here, you can see that the Deteregent.main () call to Cleanser.main () is explicitly done.
It is necessary to emphasize that all classes in cleanser are public properties. Keep in mind that if you omit all access indicators, the member defaults to "friendly." In this way, only access to package members is allowed. In this package, anyone can use a method that does not have an access indicator. For example, detergent will not encounter any problems. However, if a class from another package is prepared to inherit cleanser, it can access only those public members. So when you plan to inherit, a good rule is to make all the fields private and all the methods public (the protected member also allows the derived class to access it;). Of course, we still have to make some adjustments on special occasions, but this is not a good idea.
Note that cleanser contains a series of methods in its interface: Append (), dilute (), apply (), scrub (), and print (). Since detergent is derived from cleanser (through the extends keyword), it automatically obtains all of these methods within the interface-even if we do not see a clear definition of them in detergent. In this way, the inheritance can be imagined as "reuse of interfaces" or "regeneration of interfaces" (future implementation details can be set freely, but that is not the focus of our emphasis).
As you can see in scrub (), you get a method defined in the underlying class and modify it. In this case, we usually want to invoke the method from the underlying class in the new version. However, in scrub (), you cannot simply emit a call to scrub (). That makes recursive calls, and we don't want to see that happen. To solve this problem, Java provides a super keyword that refers to a "superclass" (superclass) from which the current class has inherited. So the expression Super.scrub () invokes the base class version of the method scrub ().
When inheriting, we are not limited to methods that use only the underlying classes. You can also add your own new method to the derived class. The practice is exactly the same as adding any other method to the normal class: simply define it. The extends keyword reminds us that we are going to add the new method to the interface of the underlying class and extend it. Foam () is a product of this practice.
In Detergent.main (), we can see that for the detergent object, you can invoke cleanser and all available methods within detergent (such as foam ()).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.