The final Java doubts

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" >import java.util.*;class j{public        static final int count = 011;} public class Seventyfirst extends j{public        static final int count = 022;        public static void Main (string[] args) {                System.out.println (count);}        } </span>

Results: 18


<span style= "FONT-SIZE:18PX;" >import java.util.*;class j{public        static final int count = 011;        Public final int Fun () {                return count;        }} public class Seventyfirst extends j{public        static final int count = 022;        public static void Main (string[] args) {                System.out.println (count);                System.out.println (New Seventyfirst (). Fun ());        }        /*public final int Fun () {                return count;        } */}</span>


If you don't comment out the fun

<span style= "FONT-SIZE:18PX;" >SEVENTYFIRST.JAVA:14: Error: Fun in Seventyfirst () cannot overwrite fun in J () public final int fun () {                 ^  overridden method is Final1 error </span>

After commenting out

Results:

18
9
See the difference, right?

Final has a different effect on methods and properties in terms of inheritance.

Methods cannot be overridden or hidden when a method is final decorated

When a property is final decorated, the method can be hidden.

As you can see in the property hide , when a property is hidden and can be accessed through super in a subclass, there is a way to provide a method that provides a final type of Get method in the parent class to return the hidden property

The final Java doubts

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.