The more common Java Super keyword _java in Android

Source: Internet
Author: User

Super is more common in Android and doesn't understand it without a Java base, so take time to learn.

It is simpler to use super in Java classes to refer to the composition of the base class, as shown in the following example:

Class fatherclass{public 
  int value; 
  public void F () { 
    value=100; 
    System.out.println 
    ("Fatherclass.value:" +value); 
  } 
 
 
Class ChildClass extends fatherclass{public 
  int value; 
  public void F () { 
    super.f (); 
    value=200; 
    System.out.println 
    ("Childclass.value:" +value); 
    System.out.println (value); 
    System.out.println (Super.value); 
  } 
 
 
public class Test1 {public 
  static void Main (string[] args) { 
    childclass cc=new childclass (); 
    CC.F (); 
  } 

The result of the final output is:

fatherclass.value:100
childclass.value:200
100

In addition, the construction in inheritance is also used to super, the specific rules are as follows:

1. The construction method of the base class must be called during the construction of the subclass.

2. Subclasses can use Super (argument_list) to invoke the constructor method of the base class in their own constructor methods.

3. If the constructor method of the base class is not displayed in the subclass's construction method, the system defaults to calling the base class's parameterless construction method.

4. If the subclass constructor does not display the call base class constructor, and the base class has no constructor without parameters, the compilation fails.

Examples are as follows: (It's best to test yourself here)

Class superclass{ 
  private int n; 
   
  Superclass () { 
    System.out.println ("Invoke superclass ()"); 
  } 
  Superclass (int n) { 
      System.out.println ("Invoke Superclass (" +n+ ")"); 
    } 
 
Class Subclass extends superclass{ 
  private int n; 
   
  Subclass (int n) { 
     
    //When no super is written in the constructor method of a subclass, the system defaults to calling the parent class with no parameter construction method 
    //equivalent here: 
    //super (); 
     
    System.out.println ("Call Suberclass (" +n+ ")"); 
    this.n=n; 
  } 
   
  Subclass () { 
    super (); 
    The parent class constructor must be invoked during subclass construction, and Super must be written in the first sentence (with father and son first) 
     
    System.out.println ("Invoke Subclass ()"); 
  } 
public class Test2 {public 
  static void Main (string[] args) { 
    Subclass sc1=new Subclass (); 
     
    Subclass Sc2=new Subclass (); 
     
   

The final result is:

Call Superclass () call
subclass () Invoke
superclass () Call
Suberclass (400)

The above is the entire content of this article, I hope to help you learn.

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.