Java Interface Understanding

Source: Internet
Author: User
Tags class definition modifiers


/*
Interface: A preliminary understanding, which can be thought of as an abstract class, is another way of expressing when all methods are abstract methods.

The interface is defined in the same format as the class, and the name of the interface needs to be capitalized.
Interface Interface Name
{
....//Interface definition.
}

The general contents of the interface are: constants, abstract methods
The members in the interface are decorated with fixed modifiers.

Constants: public static final (global constant, referenced by class name)
Abstract method: Public abstract

interface, you can define members without modifiers, because the member modifiers in the interface are fixed, with fewer modifiers, and the system defaults to adding
What modifiers. That is, the member modifier can be omitted, but in the actual development, it is best to carry all, so that the program will read better.

The meaning of an implementation in an interface, in the case of a member method, is overwrite (overwritten).

Interfaces are different from abstract classes:
The methods in the interface must be public, and the abstract
The permissions of a method in an abstract class can be set by itself, and can be abstract or not abstract.
The member variables in the interface are constants and are global constants that can be accessed with the class name
The member variable permissions in an abstract class, whether static or not, are constants that can be defined by themselves.

The relationship between an interface and a class is an implementation: denoted by the keyword implements.
The implementation without inheriting, is to distinguish with the abstract class, the subclass inherits the parent class, is to explain that some methods in the parent class can be brought directly to use.
Do not have to override (overwrite <=> implement). For subclasses to implement interfaces, all methods in the interface need to be implemented. So just use
Implementation to differentiate. Essentially implementing an interface is equivalent to inheriting from an abstract class that all methods are abstract methods. Subclasses have all of the interfaces
Content, and to implement all the abstract methods defined in the interface to create an instance (object) of the subclass, if only implementation (overwriting, overriding
) Part of the interface, the subclass must be defined as an abstract class, because the subclass implements the interface, which has a part of the abstract method.

The. class file is also formed after the interface is compiled, and the. class filename and interface name are identical. That is, the interface is represented in the form of a class.

Interfaces can be viewed as abstract classes in a sense, and implementations are equivalent to inheritance.

Multiple inheritance exists between interfaces and interfaces.
There can be multiple inheritance between interfaces and interfaces, because methods in an interface are abstract methods, only method declarations, no method bodies, so
When interface C inherits interface A, interface B, the same method exists immediately in a, B, and is only equivalent to the existence of one in C. Class D implementations
Interface C, only need to override (overwrite, implement) one time in the class definition, you can override (overwrite, implement) the two methods in the A/b.
However, it is not allowed to happen:
That is, interface A, there are two function names in interface B, the parameter list is exactly the same, but the return value is not the same function.
overriding functions and overridden functions must be exactly the same (including return values, function names, parameter lists) because of the requirements of overriding (overwriting, implementing) attributes
, the override function's permission is greater than or equal to the permission of the overridden function.
So when Class D implements interface C, it is necessary to have two function names in the definition of Class D, the same function as the argument list, but this will make an error.
So this situation is also not allowed to appear, or the compiler error.

Interfaces can be implemented by a class, where a class A can implement multiple interfaces (interface B, interface C, interface D ...). )。
A class can implement multiple interfaces because class C can implement interface A, interface B at the same time. Because even if the exact same method exists in a A, you only need to
The definition of Class C can be overridden once. The object of Class C does not appear to be problematic when it is called. In the final analysis, there is no method body in a B, only
Method definition. Class C can be implemented casually (overlay, overwrite).
Of course, the following conditions are allowed:
That is, interfaces A and B appear exactly the same way except for the return value type. Because of this, there will be two in class C except for the return value.
The others are the same two identical methods. This is not allowed and the Java compiler will give an error.

There can be only single inheritance between classes and classes in Java (there can be multiple layers of inheritance), but not multiple inheritance.
The reason: When subclass C Inherits parent Class A, parent class B, if
The object of subclass C is called when the same method exists in the parent class A and in the parent class B, or when the method name and the parameter list are the same, and the method with different values is returned.
, problems can occur. Therefore, multiple inheritance is not allowed in Java.

Multiple implementations of an interface (that is, a class can implement multiple interfaces) are the embodiment of multiple inheritance between classes in Java. Java uses multiple implementations of classes to replace classes and classes
Multiple inheritance.
Interfaces are used to increase the functionality of a class. A class can implement multiple interfaces at the same time as inheriting another class.

*/


Interface Inter
{

public static final int NUM = 4;
public abstract void Show ();

}

Class Test implements Inter
{
public void Show ()
{
}
public void Speak (), after the function parenthesis (), or {}, when it is, must be declared as an abstract function.
A method in a class that either has a method body or is abstract. Non-abstract functions that do not have a method body are not allowed.
followed by any {}, and the correct wording, but generally omitted.
}

Interface A
{
public abstract void MethodA ();
}
Interface B//extends A
{
public abstract void MethodB ();
}
Interface Cextends B,a
{
public abstract void methodc ();
}
Class D implements C
{
public void MethodA () {}
public void MethodB () {}
public void Methodc () {}
}

Interface Intera
{
public abstract void Show ();
}
Interface Interb
{
public abstract void Show ();
}
Interface Interc extends Intera,interb
{
public abstract void speak ();
}
Class TestD implements INTERC
{
public void Show () {}
public void Speak () {}
}
/*
Error Example:
Interface A
{
public abstract int Show ();
}
Interface B
{
Public abstract Boolean show ();
}
Class C implements A, b
{
public int Show () {}
public boolean Show () {}
}
Class D
{
public static void Main (string[] args)
{
c C = new C ();
}
}

*/

Class Interfacedemo
{
public static void Main (string[] args)
{
Test T = new Test ();
System.out.println (T.num);
}
}

Java Interface Understanding

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.