Sun certification Study Notes series-statements and access control (2)

Source: Internet
Author: User

Interface implementation

Interfaces are the contract of what classes can do, but they do not specify the implementation method of class operations!

The interface can be implemented by any class in the inheritance tree

An interface is like an abstract class. Whether or not abstract modifiers are used in the Declaration, they are implicitly abstract.

An interface can only have abstract methods, but not specific methods.

APIs are public and abstract by default. explicit declarations of these modifiers are optional.

Interfaces can have constants, which are implicitly public, static, and final.

Public, static, and final interface constant declarations can be any combination

A valid Non-Abstract implementation class has the following attributes:

It provides specific implementation for all methods from the interface

The methods implemented by this method must comply with all valid rewrite rules.

It must not declare any new check exceptions for the implementation method.

It must not declare a check exception that is wider than the exception declared in the interface method.

Regardless of the interface declaration, it can declare runtime exceptions in any interface method implementation.

It must contain the method signature and return type exactly the same as the method implemented by it (but it does not need to declare the exception of this interface)

Classes implementing interfaces can be abstract themselves

Abstract implementation classes do not need to implement interface methods (but the first concrete subclass must be implemented)

Class can only expand one class, but it can implement multiple interfaces

Interface can expand one or more other interfaces

An interface cannot expand a class or implement a class or interface.

Member access modifier

Methods and instance variables (non-local) are called "members"

Members can use four access levels: Public, private, protected, and default

There are two forms of member access:

The code in the class can access members in another class.

Sub-classes can inherit the members of their superclasses.

If you cannot access the members class, you cannot access its members.

Determine the class visibility before determining the Member visibility

Public members can be accessed by all other classes, or even classes in different packages.

If the superclass member is public, the Child class inherits from it no matter which package it is in.

No. Operator access members must be in the same class

This. Only the currently executed object is always used.

This. amethod is exactly the same as calling only amothod.

Private Members can only be accessed by code in the same class.

Private Members are invisible to child classes, so they cannot inherit private members.

The difference between default and protected members is that when the subclass is called;

By default, members can only be accessed by other classes in the same package.

Protects members from being accessed by other classes in the same package and by sub-classes in any package.

Protection = package + descendant

For out-of-package subclasses, only access protection members can be inherited; for out-of-package subclasses, access protection members cannot be accessed by reference to super-class instances;

In fact, the protection members inherited by subclass from other packages are private to other classes.

Local variable

The local variable declaration cannot have an access modifier

Final is the unique access modifier available with local variables.

The local variable has no default value. Therefore, you must initialize it before use.

 

Other modifiers -- members

The final method cannot be rewritten in the subclass.

The abstract method ends with a semicolon instead of braces.

There are three methods to identify non-Abstract METHODS

This method is not marked as abstract

This method has braces

This method has code before two braces

To expand abstract, the first non-abstract class must implement the abstract method of all abstract classes.

The synchronized modifier can only be used for methods and code blocks.

The synchronized method can have any access control, including final

Abstract methods must be implemented by the quilt class. For this reason:

Abstract methods cannot be private

The abstract method cannot be final.

Native modifiers can only be used and used

The strictfp modifier can only be used with classes and methods.

 

Methods With VaR-Arg

From Java 5, the method can declare a form that accepts zero or multiple numbers, that is, the so-called var-Arg method.

VaR-Arg is declared using the syntax type... name, such as dostuff (Int... x ){}

The VaR-Arg method can contain only one var-Arg parameter.

In a method with a normal parameter and a var-Arg parameter, the VaR-Arg parameter must appear at the end.

Variable Declaration

Instance variables can be --

Has any access control

Marked as final or transient

Instance variables cannot be labeled abstract, synchronized, native, and strictfp

It is legal to declare a local variable with the name that is the same as the instance variable. This is called "hidden"

The final variable has the following attributes:

After a value is assigned, the final variable cannot be reinitialized.

Once an object is assigned to a final variable, different variables cannot be referenced in the final referenced variable.

The final variable must be initialized before the constructor is complete.

There is no such statement as a final object. The reference marked as final does not mean that the object itself is immutable.

The transient modifier can only be used for instance variables.

The volatile modifier can only be used with instance variables!

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.