Java Learning Summary (iv)

Source: Internet
Author: User
Tags modifier

1. Interface interface1.1 syntax: Modifier Interface Interface Name {
Constant
are all abstract methods
} Modifier: Default | Public
Interface Name: Custom, the rule is the same as the class name, usually I begin with the letter: WiFi Interface (IWIFI)
// Example:     Public Interface iwifi{        int count=1;        publicabstractvoid Send (); // transmit public        void receive ();// receive             }
1.2 interface differs from class: an interface cannot be used to instantiate an object.
Interface has no constructor method.
All methods in an interface must be abstract methods.
An interface cannot contain member variables, except static and final variables.
Interfaces are not inherited by classes, but are implemented by classes.
The interface supports multiple inheritance. 1.3 Interface Features: 1. Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract (only public abstract, and other modifiers will be error-marked).

2. The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, with the private decoration will report compilation errors.)

3. The method in the interface cannot be implemented in the interface, only by implementing the interface class to implement the method in the interface.

1.4. Why interface inheritance is required: Describe the natural properties of things and the reuse of behaviors. Interface: Describes the social properties and the reuse of the behavior of a thing. 1. Importance: In the Java language, abstract classes and interface are the two mechanisms that support the definition of a class of abstractions. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability. 2, simple, normative: If a project is large, then you need an architect who can clear all the business to define some of the main interfaces, these interfaces not only tell the developers you need to implement those business, but also the naming specification is limited (to prevent some developers can not see the name of the other programmers do not understand). 3, Maintenance, Expansion: For example, you want to do an artboard program, which has a panel class, mainly responsible for painting functions, and then you define this class. 4, security, tightness: interface is an important means of software loose coupling, it describes all the services of the system outside, and does not involve any specific implementation details. This is more secure, more rigorous (the General software service providers consider more). Because a class has a "single root", all classes can have only one direct parent class, and multiple inheritance is possible by implementing a class that has more than one parent class.one of the Java features: single inheritance, but multiple interfaces can be implemented.

1.5, the interface features 1), the interface method can have a parameter list and return type, but cannot have any method body. 2), the interface can contain fields, but they are implicitly declared static and final. 3), the field in the interface is stored only in the static storage area of the interface, not the interface. 4), the method in the interface can be declared public or not declared, but the result will be treated according to the public type. 5), when implementing an interface, the defined method needs to be declared as public type, otherwise the default access type, the Java compiler does not allow this situation. 6), if you do not implement all the methods in the interface, then the creation is still an interface. Subclasses must implement methods that are not implemented in the interface, unless subclasses are also interfaces. 7), extending an interface to generate a new interface should use the keyword extends to implement an interface using implements. 8), the method in the interface is abstract method (abstract), can not be static method (static), all the methods of the interface are abstract, and the abstract method is not static, there is static method is not override, so it is meaningful to define the interface. The field in the interface is the default: Static final, which is commonly said to be constant.

1.6 Class How to implement an interface:
// Example:     Public class Implements iwifi{        publicvoid  send1 () {            System.out.println ("Send information with WiFi");        }          Public void receive1 () {            System.out.println ("receive information with WiFi");        }          Public void Send () {            System.out.println ("short with mobile phone");        }    }
Attention:
1. If the class does not implement all of the abstract methods, the class must be defined as an abstract class implements keyword
2. Interface can be inherited by the quilt interface, using extends keyword
3. Subclasses can inherit a parent class, implementing multiple interfaces
4. An interface cannot instantiate an object, but an interface type variable can refer to an implemented subclass object
// Example:     Public Interface iusb2{        publicabstractvoid  send2 ();          Public void receive2 ();    }      Public Interface extends iusb2{        publicabstractvoid  connection (IUSB3 USB);            }
//Example: Inheriting a parent class, implementing multiple Interfaces Public classcomputer{ Public voiddownLoad () {} Public voidupLoad () {} ...} Public classDell_computerextendsComputerImplementsiwifi,iusb3{ Public  voidconnection (IUSB3 ortherusb) {System.out.println ("This<-->ortherusb"); System.out.println ("Connect USB3.0 Settings"); }     Public  voidSend1 () {System.out.println ("Send messages with WiFi"); }     Public voidreceive1 () {System.out.println ("Receive information with WiFi"); }     Public  voidSend2 () {System.out.println ("Send message with USB3.0"); }     Public voidreceive2 () {System.out.println ("Receive information with USB3.0"); }}
an interface cannot instantiate an object, but an interface type variable can reference an implemented subclass objectIwifi WiFi Device 1 = new Mobile ();
Mobile phone = new mobile ();
WiFi device 1 Accessible methods: Send1 () and receive1 ();
Mobile phone can access the method: Send1 (), receive1 (), send (); Iwifi WiFi Device 2 = new Dell_computer ();
IUSB2 Device 3 = new Dell_computer ();
IUSB3 Device 4 = new Dell_computer ();
Computer computer = new Dell_computer ();
Dell_computer notebook = new Dell_computer (), WiFi device 2 can access the method: Send1 () and receive1 ();
Device 3 can access methods: Send2 () and receive2 ();
Device 4 Accessible methods: Send2 (), receive2 () and connection (parameters);
Computers can access the method: DownLoad () and upload ();
Notebooks can be accessed by: Send1 (), receive1 (), Send2 (), Receive2 (), connection (parameters), downLoad () and upload ();

Abstract class 2.1, the syntax defines the abstract class definition, the abstract class is used before the Abstraction keyword decoration, then the class is an abstract class. 2.2, use a, in some cases, a parent class just know how its subclasses should contain the method, but do not know exactly how these subclasses implement these methods
(Abstract class constraints subclasses must have methods, but they are not concerned with how subclasses implement these methods.) b, from a number of classes with the same characteristics abstract an abstract class, the abstract class as a subclass of the template, so as to avoid the arbitrary design of sub-class. 2.3. The meaning limit specifies that subclasses must implement certain methods, but do not pay attention to implementation details. 2.4, characteristics 1, the abstract method must be in the abstract class 2, the abstract method and the abstract class must be modified by the abstract keyword 3, abstract class can not be created with new object. Because invoking abstract methods does not make sense 4, abstract methods in the abstract class to be used, the subclass must be replicated all the abstract methods after the child class object call. If a subclass overrides only a subset of the abstract methods, then the subclass is an abstract class. 5. Abstract method without method body, end with semicolon
//Example Packagecom.zhangguo.chapter5.s2;ImportJava.util.Scanner;/**Animal*/ Public Abstract classAnimal {/**name*/     PublicString name; /**abstract method, no method body, must quilt class implementation (override)*/     Public Abstract voideat (); /**Test*/     Public Static voidMain (string[] args) {//LSP Richter Replacement principleAnimal dog=NewDog (); Dog.name= "Bo Mei"; //int i=1; //Scanner input=new Scanner (system.in);dog.eat (); }        /**Abstract classes can have non-abstract methods, can have static methods*/     Public voidShow () {};}/**subclasses of abstract class animals (Animal) must implement methods that are not implemented by the parent class*/classDogextendsAnimal {//Annotations@Override Public voideat () {System.out.println ( This. name+ "The dog is eating bones."); }}

Operation Result:

2.5 The difference between abstract classes and interfaces: Similarities and differences:
1. The hierarchy of inheritance is the upper layer.
2. You cannot instantiate an object.
3. Interface all methods are abstract, variables are public static final types, abstract classes can have abstract methods and constants.
4. An abstract class can only be used as a unique parent class, and an interface may implement more than one or be inherited.
5. The abstract class is defined by the abstract class, and the interface is defined by interface.
6. The parent class is the relationship of "is a", the parent class is an abstract class, and the parent class is the "like a" relationship, and the parent class is the interface.

Third, final (final) 3.1, final modified class final decorated class is not allowed to be inherited. A class cannot be both final and abstract. Because the primary purpose of abstract is to define a convention that allows subclasses to implement such conventions, and final indicates that the class cannot be inherited, and that the two contradictions.

3.2. Final modification method, which means that the method cannot overwrite override in the method of the quilt class. Cannot be rewritten

3.3. Final modified variable The final member variable represents a constant,can only be assigned once, the value will not change after the assignment。  When final modifies a native data type, the value that represents the native data type cannot be changed, and if the final decoration of a reference type means that the reference type cannot point to another object, the contents of the object to which the reference refers to can vary.  is essentially one thing, because the value of the reference is an address, the final requirement value, that is, the value of the address does not change.  Final modifies a member variable (property) that must be displayed for initialization.  There are two types of initialization, one is initialized at the time of the variable declaration, and the second is to not assign the initial value when declaring the variable, but to assign an initial value to the variable in all the constructors of the class where the variable resides. When the parameter type of a function is declared final, the parameter is read-only.

  

Java Learning Summary (iv)

Related Article

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.