Inheritance of Java

Source: Internet
Author: User

1. Initialization Order of inheritance:

Parent class, parent class, properties in the initialization object of the parent class---the constructor method of the property, child class, subclass

If there is a constructor: Execute the property first, then the construction method

If the Name property is not assigned to the constructor, the value of name is the value assigned to the Class property

 package   COM.WANGCF;  //  parent class  public  class   Animal { public  String name= "Initialization of properties" ;  public  int      age;  public  void      eat () {System.out.println ( "animals can Eat"  public   Animal () {Name  = "Construction method        The initialization of ";    System.out.println ( "The construction method of the parent class performed the" 
 package   COM.WANGCF;  //  subclass  public  class  Bridge extends   animal{ public  String name=" sub-class Property ";  public   Bridge () {System.out.println (    span> "The constructor of the subclass" ); }}
 Package COM.WANGCF; // Test Class  Public class Test {    publicstaticvoid  main (string[] args) {        Bridge bri= New  Bridge ();  Test initialization Sequence        System.out.println (bri.name); The assignment method of the test attribute is        System.out.println ("=============");        Animal ani=new  Animal ();        System.out.println ("Output value:" +ani.name); Test the assignment of a property    }}

2.fianl

Class can be decorated, the class cannot be inherited

You can modify the method, the method cannot be overridden

Properties can be modified: the properties of the class are not implicitly initialized, that is, the final int a=1 must be assigned, correct; fianl int A; error

or assign a value in the constructor method

Choose one of these two ways

Variable can be modified: it can only be assigned once, then it is constant.

Inheritance of Java

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.