Cainiao learning JAVA (1) -- class, inherited keywords

Source: Internet
Author: User

Recently I learned JAVA for Android, And I encountered some problems in this section:

1. extends and implements

Extends inherits the parent class and implements interfaces. JAVA does not have multiple inheritance, but multiple interfaces can be implemented.

Reference: What is the difference between extends and implements in JAVA?

To implement an interface, you must implement all the methods of this interface.(Except abstract classes ).
B. Methods in interfaces are abstract.
C. Multiple irrelevant classes can implement the same interface, and one class can implement multiple irrelevant interfaces.

Class A extends B implements C, D, E

Will overwrite the variables or functions defined by the parent class.. The advantage is that the architect defines interfaces so that engineers can implement them. The development efficiency and cost of the entire project are greatly reduced.

Subclass cannot overwrite the methods or variables of the parent class. Even if the subclass defines the same variables or functions as the parent class, it will be replaced by the parent class.

Extended question: Why is interface in JAVA not in C ++?

Why is there an interface in Java?

-If you give an interface, but leave some without definition, this part is implemented by its derived class. The interface keyword is created. It is not only an extreme form of abstract classes, but also a means for you to upload an object to multiple base classes. .

Interface is a further enhancement of abstract keywords,It allows you to define the form of A Class: method name, parameter list, and return value. Its methods do not need to be declared as public. An Interface can also contain data members, but it is inherently static and final. To be precise, it is not a component of an Interface, but saved in the dynamic storage area of this Interface. The Interface means that the class of the interface "all" and "implement" should look like this, as long as this Interface is used, it knows which methods can be called. When you implements an interface, you must define the interface method as public. Otherwise, the java compiler will report an error.

Because the interface does not carry any implementation-that is, the interface has nothing to do with the memory-no one will block the combination of interfaces.. This is very important, because sometimes you will encounter "x is both a, B, or c", which combines multiple interfaces, called "Multi-inheritance ". Java does not force you to inherit abstract or "concrete" classes, but you can only inherit one non-interface class. All the basic class elements must be interfaces.

Extends can be used to represent multiple "base interfaces", which must be separated by commas.

 

2. Functions and usage of inner class

TBD

3. Differences between inheritance rules (public/private/protected) and C ++ in JAVA

TBD

4. Call Sequence of JAVA constructor and Android onCreate Function

TBD

5. @ override and super

TBD

6. abstract and final

TBD

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.