Interfaces in Java

Source: Internet
Author: User

1. What is an interface?

Answer:1). Java Interface is the declaration of a series of methods , is a collection of some abstract;

2). an interface only abstract methods do not have the implementation of methods, so these methods can be implemented in different classes, and the implementation of these methods can have different behavior (function).

3). simply put, an interface is a special abstract class, and all methods are abstract classes of abstract methods Java in the interface ( Interface ).

2. What is included in the interface? What's the difference? Examples Show

Answer: [ modifier ] Interface Interface Name [extends parent interface List ]{

[Public] [Static] [Final] Constants ;

[Public] [Abstract] Method ;

}

1). modifier: Optional, to specify access permissions for the interface, optional value Public . Even if omitted, it is still public.

2). Interface Name: Required parameter to specify the name of the interface, the interface name must be legal Java identifier. In general, the first letter is required to capitalize.

3). Extends Parent interface Name list: Optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter. The parent interface can exist multiple, separated by commas.

4). method: The method in the interface is only defined and not implemented.

3. What do I need to be aware of when using interfaces?

Answer:1). An interface is a special kind of abstract class.

2). There are no variables in the interface, only Public static Final the static constant of the adornment. Three modifiers can be omitted.

3). all methods in an interface are abstract methods, and the default is Public permissions.

Example:public interface Flayer {

public static final int TYPE = 1;

public abstract void Fly ();

public abstract void land ();

public abstract void takeoff ();

}

4. Why use classes to implement interfaces?

answer : 1). classes implement interfaces, essentially similar to class-inheriting classes, except that "classes inherit at most one class, i.e. single-class, while one class can implement multiple interfaces at the same time, and multiple interfaces are separated by commas." The implementation class needs to overwrite all the abstract methods in all interfaces, otherwise the class must also be declared as an abstract class.

2). Interfaces are abstract classes, and interfaces do not have any specific methods and variables, so interfaces cannot be instantiated. An interface defines an operation that is implemented by multiple classes, that is, "what does." Classes can implement interfaces to override methods in the interface to implement "how-to-do".

5. How do I implement an interface? What is included in the content?

Answer: [ modifier ] class < class name > [Extends parent class name ] [implements interface list ]{}

1). modifier: Optional parameter that specifies the access rights for the class, with an optional value of Public , Abstract and the Final .

2). class Name: Required parameter, which specifies the name of the class, and the class name must be legal Java identifier. In general, the first letter is required to capitalize.

3). Extends parent class name: Optional parameter that specifies which parent class the class to define inherits from. When you use the extends keyword, the parent class name is a required parameter.

4). Impelments interface list: Optional parameter that specifies which interfaces the class implements. When using the Implements keyword, the interface list is a required parameter. When there are multiple interface names in the interface list, separate the interface names with commas.

6. How do I use interfaces to inherit interfaces? What do I need to pay attention to?

Answer:1). the Java interface inherits the form of an interface:

Example: Interface3 extends Interface0, interface1,interface ...

2). *java interface can inherit multiple interfaces

* interface Inheritance interface still use keyword extends, do not use the wrong implements

7. What is the difference between interface inheritance and class inheritance?

Answer:1). the inheritance of Java classes is single inheritance, and The inheritance of Java interfaces is multiple inheritance.

8. Why does the interface enable multiple inheritance ?

Answer:1). The main reason for not allowing class multiple inheritance is if A Simultaneous inheritance B and the C , while B and the C There is also a D method, A It is not possible to determine which one to inherit.

2). interfaces are all abstract methods inherited by anyone, so interfaces can inherit multiple interfaces.

9. What is the difference between an abstract class and an interface?

Answer:

Abstract class

Interface

Property

No restrictions

Static constants

Construction method

Dispensable

No

Common methods

There can be a concrete way

Must be an abstract method

Sub-class

Single inheritance

Multiple implementations

1). The abstract class represents an inheritance relationship in the Java language.

A class can only use one inheritance relationship at a time. However, a class can implement multiple interface.

2). in the Abstract class can have their own data members, or can have non- Abstract the member method, while the Interface can only have static data members that cannot be modified ( i.e. must be static final , but data members are not generally defined in interface ) , all the member methods are abstract .

3). classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

4). the variables defined in the interface are by default Public static Final And must give its initial value, so the implementation class cannot be redefined and its values cannot be changed.

5). the methods in the interface are, by default, public,abstract type of.

Why should I use an interface?

1). interfaces are design-level concepts that are often designed by designers to separate definitions from implementations.

2). programmers implement interfaces to implement specific methods.

3). Common class design Structures

What is interface-oriented programming?

Answer:1). interface-oriented programming and object-oriented programming are not lateral, it is not an independent idea that is more advanced than object-oriented programming, but is attached to the object-oriented ideology, which belongs to its part.

2). or, it is one of the essence of thought in object-oriented programming system.

3). interface-oriented programming means that the interaction between all classes or modules in an object-oriented system is done by the interface.

How do I use the comparable interface?

answer : 1). The comparison method is defined inthe comparable interface,Comparableto, and the return value is int ( ignoring generics, subsequent learning ) .

2). when int The value is positive, represents greater than, is negative, is less than; 0 , which means equals.

3). There are many classes in the API that implement this interface, and many methods use that interface.

4). by using Arrays.sort (object[]) method that understands the role of the interface and further understands the role of the interface.

Interfaces in Java

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.