When asked "Why the variables in the interface are final decorated"

Source: Internet
Author: User
Tags closure
When I met this problem today, it is really difficult to understand, only know that the variables in interface by default are modified by public static final, the method in the interface is decorated by public and abstrct. Consulted a lot of information, did some examples, came to the following conclusions, the lack of places to want to point out.

Java code           /*        *  About abstract classes and interfaces        *         * 1. When defining variables        *  an abstract class-defined variable can be unassigned.        *         *  The variables defined in the interface (specifically, constants) must be assigned (final).        *        * 2. When defining methods         *         *  Abstract classes are defined in the method, you can specify the program body can also not be specified.        *  programmers can specify program body &nbsp in Non-abstract methods, similar to methods in other normal classes.        *  But in abstract methods, you must not specify the program body (write program experience reported: Abstract methods do not  specify a body error), similar to the form defined in the interface.        *         *  interface method, all methods are default to abstract, Therefore, the program body must not be specified.        *         * 3. Design Intent        *         *  abstract classes are used by Java design to implement inheritance. The        *  interface is a special case for multiple inheritance.        *         * 4.is a     and   like a (to be looked at)        */        public abstract class abstratclasstest {            String name ;            //by final variable, but cannot specify a valid set method (final action reason), Does not embody the closure principle of encapsulation.            final static int age =1;                  //       public void setage () {  //          this.age = 2;   //      }                         public static int getage ()  {               return age;            }                public void test () {                            }                         public  abstract void test1 ();                    } &NBSp
		* *
	 about abstract classes and Interfaces
	 * * 
	 1.
	 * The variables defined by the abstract class can be assigned without assigning values.
	 * 
	 * The variables defined in the interface (specifically, constants) must be assigned (final).
	 *
	 2. The method defined in
	 the 
	 abstract class when defining a method can specify that the program body can also be unspecified.
	 * Programmers can specify a program body in a Non-abstract method, similar to the methods in other ordinary classes.
	 * However, in abstract methods, you must not specify the body of the program (the writer has reported: abstract methods do specify a), similar to the form defined in the interface.
	 * 
	 * The method in the interface, all methods are abstract by default, so the program body must not be specified. * * 
	 3. Original design
	 * 
	 Abstract class is designed to implement inheritance by Java.
	 * Interfaces are a special case for multiple inheritance.
	 * 
	 * 4.is a   and like  A (see also) */
	 public
	abstract class Abstratclasstest {
		String Name;
		The final variable, but cannot specify a valid set method (the reason for final action), does not embody the closure principle of encapsulation.
		final static int age =1;

		
Public		void Setage () {
//			this.age = 2;		public
		
		static int getage () {return age
			;
		}

		public void Test () {
			
		} is public
		
		abstract void test1 ();
		
	}



Java code  /*    *  Why variables in interfaces are final modified (constant)    *  because the interface itself is used to define a standard, specification, is used to design user-available details (public) and cannot be changed at will (final)    *  then everything in the interface should be defined as unified, then the defined value should be constant    *  Think again: If the interface can be defined as not final, but only public and static modified, then the implemented class can be arbitrarily modified (in the implementation of the static block in the Class), which will cause the standard is not uniform    *  (is not a bit far-fetched.) )。      *     *  interface or do not arbitrarily change, if you can change at will cause a lot of trouble, such as an interface is implemented by many classes, then modify the interface, you must also modify the implementation class, the degree of trouble is self-evident.    */      public interface interfacetest {          String name =  "Tom";                public static final int  age = 2;           public void test ();           public abstract void test1 ();      }  
*
 * Why the variables in the interface are final decorated (constant)
 * Because the interface itself is used to define a standard, specification, which is used to design user-available details (public) and not be arbitrarily changed (final)
 * Then anything in the interface should be defined as unified, then the value defined should be constant
 * Think again: If the interface can be defined as not final, but only public and static modified, then the implemented class can be arbitrarily modified (implement the static block in the Class), This will cause the standard is not unified
 * (is not a bit far-fetched.) )。 
 * 
 * interface or do not arbitrarily change, if you can change at will cause a lot of trouble, such as an interface is implemented by many classes, then modify the interface, you must also modify the implementation class, the degree of trouble is self-evident.
 * * Public

interface Interfacetest {

	String name = "Tom";
	
	public static final int age = 2;

	public void Test ();

	public abstract void Test1 ();

}




An example or a rebuttal: a bank teller machine
The card port in the bank's ATM machine can be viewed as an interface, and all ATM slotting ports are bound by a uniform standard (size size) to facilitate the insertion of each card into each ATM. If you can easily modify the number of card port interface, a row in the local modification of the size of the card mouth, when you go to the ATM to withdraw money, but found that the card mouth small point, then ...
From the example above, it is meaningful to define the values in an interface as constants. And it is proved that interface is better than abstract class at this time.

Conclusion: Because the interface itself is used to define a standard, a specification that is used to design the user's available details and cannot be arbitrarily changed, uniform values, so it is best to decorate with final.

Reference documents:
1.http://forums.sun.com/thread.jspa?threadid=741407&messageid=4251167
2.http://www.7880.com/info/article-538dc540.html

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.