Package. Import. Class in Java and comparison with C ++

Source: Internet
Author: User

Two keywords extends: Generally the inheritance class, which cannot be inherited multiple times in java. Implements: generally refers to the implementation interface. Because java does not support multiple inheritance, an interface is used to implement a function similar to multiple inheritance. Example: [java] class Son extends Father implements InterfaceA, InterfaceB, InterfaceC class Son extends Father implements InterfaceA, InterfaceB, InterfaceCson class, inherit from father class, and implement three interfaces at the same time. Abstract classes are similar to classes in C ++ that contain pure virtual functions. They cannot be instantiated or inherited. Abstract classes are modified in abstract. Abstract methods are also modified using abstract. abstract methods do not have a method body. This is similar to pure virtual functions in C ++, but "= 0" is not required here ". [Java] abstract class A // abstract class {abstract void doSomething (); // abstract method public void doOtherthing () // non-abstract method {int a = 0 ;}} abstract class A // abstract class {abstract void doSomething (); // abstract method public void doOtherthing () // non-abstract method {int a = 0 ;}} abstract classes can have non-abstract methods or their own data members. Abstract classes are generally used to define some methods and then let others implement them, which is similar to the general design diagram. The internal class is relatively simple, that is, the class defined inside the class, four types: regular internal class, local internal class, anonymous internal class, static nested internal class. Normally, it is defined directly in the class, and there cannot be static members. You can use any data of the external class just like using your own class. The local data is defined in the method, similar to local variables, the class is declared first, and then instantiated. The anonymous internal class appears together with the class declaration and definition, for example: A a = new A () {public void f () {int a = 0 ;}} can even be used as a function parameter, which is commonly used in Android development. static nested internal classes are similar to the conventional ones, but static is added, you can only access static members of external classes, but not instance members. Interfaces are modified using interfaces. The methods are abstract methods. abstract methods can be used or not used. A common class must implement all the methods in the interface class. [Java] interface class A // interface class {void doSomething (); // abstract methods do not need abstract modification} interface class A // interface class {void doSomething (); // abstract methods do not need to be abstract.} All methods in the interface class are abstract methods, and the Members must be static and immutable, that is, final static. Interface classes are generally used by people. They are similar to program plug-ins. You can choose which plug-ins to use or which plug-ins to use. Some non-Abstract methods of interface classes and abstract classes act as data members. abstract classes can have similar design drawings and design methods, so that others cannot implement interface classes. They can all be abstract methods, but it must be a static and immutable plug-in for similar programs.

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.