The interface and polymorphism of Java JDK7 Learning Note

Source: Internet
Author: User
Tags define abstract

1, for the "definition of Behavior" is the interface, you can use the interface keyword definition, the interface method can not operate, directly marked as abstract, and must be public modified.

class to manipulate interfaces, the Implements keyword must be used. When you operate an interface, there are two ways to handle the methods defined in the interface, one is the method defined in the Operation interface, and the other is to mark the method as abstract again.

2, in Java semantics, inheritance will have a "is a" relationship, the operating interface is said to "own behavior", but there will be no "is a" relationship. For the syntax of the interface polymorphism, the way is "the right side is not the behavior of the left", or "the right object is not the operation of the left interface."

3, the class can operate more than two classes, that is, can have more than two kinds of behavior. A class can inherit a class at the same time and manipulate some interfaces. An interface can inherit from another interface, that is, inherit the parent interface behavior, and then define the behavior in the sub-interface.

4, use interface to define abstract behavior appearance, method to declare as public abstract, no need and cannot have operation. For convenience, you can omit public abstract, and the compiler will automatically complement it.

5. You can use an interface enumeration constant, only defined as public static final. For convenience, public static final can be omitted, and the compiler can do it by itself.

6, if there are two interfaces have defined a method, the operation of the two interface class and there will be no error, so can be compiled, but in the design to think about: Two interfaces have a defined method to indicate a different behavior? If the same behavior can define a parent interface, if a different behavior, you have to change the method name of a behavior.

7, the interface can inherit other interfaces, can also inherit more than two interfaces, also use the extends keyword, which represents the behavior of inheriting the parent interface.

8, if there is a temporary inheritance of a class or operation of an interface and establish the requirements of the instance, and such subclasses or interface operation classes are used only once, do not need to define names for these classes, you can use the anonymous inner class to solve this requirement. The syntax for an anonymous inner class is:

New parent class () | Interface () {

Class Ontology operations

};

9. The enum syntax has been added since JDK5 and can be used to define enumeration constants. Enum defines a special class that inherits from Java.lang.Enum, but is handled by the compiler and the direct writer inherits the Enum class and is rejected by the compiler.

10. The enum actually defines the class, and the constants enumerated in the enum are actually public static final, and as an enumeration type instance, it is not possible for the writer to instantiate the enumeration type directly because the constructor permission is set to private, only the class can be instantiated.

After-school practice selection Questions:

1, Interface some{

protected void Dosome ();

}

The following description is correct: the compilation failed.

Note: The behavior method in the interface defined with interface is public abstract by default and will be error-filled using other compiler programs.

2, Interface some{

int x = 10;

}

public class main{

public static void Main (string[] args) {

System.out.println (some.x);

}

}

The following description is correct: Display 10.

Note: In Java classes, interfaces can be used directly to invoke constants directly instead of implementing interfaces to reveal the implementation behavior to the outside world.

3, Interface some{

void Dosome ();

}

Class Someimpl Implments some{

void Dosome () {

System.out.println ("Do something");

}

}

public class main{

public static void Main (string[] args) {

Some s = new Someimpl ();

S.dosome ();

}

}

The following description is correct: the compilation failed.

Note: Because the class implements the interface and once again defines the behavior method within the interface, the method's modifiers must be public.

4, Interface some{

void Dosome ();

}

Class Someimpl Implments some{

public void Dosome () {

System.out.println ("Do something");

}

}

public class main{

public static void Main (string[] args) {

Some s = new Someimpl ();

S.dosome ();

}

}

The following description is correct: show "do something".

5, Interface some{

void Dosome ();

}

Interface other{

void DoOther ();

}

Class Someotherimpl implements some,other{

public void Dosome () {

System.out.println ("Do something");

}

public void DoOther () {

System.out.println ("Do other things");

}

}

public class main{

public static void Main (string[] args) {

Some s = new Someotherimpl ();

S.dosome ();

Other o = (other) s;

O.doother ();

}

}

The following description is correct: show "do Something", "do other things".

6, Interface some{

void Dosome ();

}

Abstract class Abstractsome implements some{

public abstract void Dosome ();

public void Doservice () {

System.out.println ("Do some services");

}

}

public class main{

public static void Main (string[] args) {

Abstractsome s = new Abstractsome ();

S.doservice ();

}

}

The following description is correct: the compilation failed.

Note: Abstract classes cannot be instantiated, but you can create instances to use as anonymous inner classes.

7, Interface some{

void Dosome ();

}

Abstract class Abstractsome implements some{

public abstract void Dosome ();

public void Doservice () {

System.out.println ("Do some services");

}

}

public class main{

public static void Main (string[] args) {

Abstractsome s = new Abstractsome () {

public void Dosome () {

System.out.println ("Do something");

}

public void Doservice () {

}

};

S.doservice ();

}

}

The following description is correct: No information is displayed at execution time.

8, Interface some{

void Dosome ();

}

public class Main {

public static void Main (string[] args) {

Some s = new Some () {
public void Dosome () {
System.out.println ("Do something");
}
public void Doservice () {
System.out.println ("Do some services");
}
};
S.doservice ();
}
}

The following description is correct: the compilation failed.

Note: the Doservice () method is not declared in the some interface.

9, Interface some{

protected static final int x = 10;

}

The following description is correct: the compilation failed.

Note: Because constants defined in an interface can only be public static final adornments.

10, Interface some{

void Dosome ();

void Doservice () {

System.out.println ("Do some services");

}

}

The following description is correct: the compilation failed.

Note: Because the behavior method defined in the interface cannot have a specific implementation.

The interface and polymorphism of Java JDK7 Learning Note

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.