Implementing object-oriented programming with Java--fourth chapter exception

Source: Internet
Author: User
Tags constant definition modifier

1. interface: interface is to give some methods without content, encapsulated together, to a class to use, in accordance with the specific circumstances of these methods written.

Interfaces are abstract classes that are more abstract,

Methods in an abstract class can have a method body,

All methods in the interface have no method body.

The interface embodies the design idea of multi-state and high-cohesion low-coupling in programming.

An interface is a specification and a standard that they can constrain the behavior of a class, which is a collection of method features, but no method is implemented.

The interface can be regarded as a special "abstract class", but it is represented by the different syntax of the abstract class.

Abstract class facilitates the reuse of code, the interface facilitates the extension and maintenance of code;

Features of the interface:

Interface can not be instantiated

An implementation class must implement all the methods of an interface//often used as a type

Implementing a class can implement multiple inheritance in many interfaces//java

The variables in the interface are static constants, (properties are global static constants, methods are global abstract methods)

Interfaces have better features than abstract classes:

1. can be multiple inheritance

2. Design and achieve complete separation

3. more natural use of polymorphism

4. Easier to build a program framework

5. easier to replace the implementation

Interface considerations:

1 , interfaces cannot be instantiated

2 , all methods in an interface cannot have a principal. Error syntax example: void aaa () {}← (note cannot have curly braces), interfaces can be considered abstract classes that are more abstract.

3 , a class can implement multiple interfaces, non-abstract class implementation interface must implement all the methods in the interface (abstract class facilitates code reuse, interface for code maintenance)

4 , the interface can be defined constants (int a=1;//plus no static are static, cannot be modified with private and protected), cannot define variables

★ There can be variables in the interface [but the variables cannot be modified with private and protected], the variables in the interface are essentially static and final type, whether you add static modifiers or not.

Access form: interface name. variable name

★ In Java, the commonly used variables, defined in the interface, as a global variable use, the interface properties are automatically with public static final decoration, that is, the interface properties are global static variables;

5 , an interface cannot inherit other classes, but it can inherit other interfaces

6 , the interface name rule is the same as the class, public is visible throughout the project, and if omitted, it can only be visible in the current package;

7 , an important principle: When a class implements an interface that requires the class to implement all the methods of the interface, otherwise it must be defined as an abstract class;

8 , all the methods in the interface are abstract methods, and the methods in the interface automatically call the public abstract decoration, that is, only the global abstract method in the interface;

9 , and abstract classes, interfaces can not be instantiated, interfaces can not have a construction method;

Ten , the interface can realize the inheritance relationship through extends, an interface can inherit multiple interfaces, but cannot inherit the class;

JAVA definition syntax for the interface:

modifier interface Interface name extends parent interface 1, parent interface 2,...... {

// constant definition

// method definition

}

                                Use interface: EG:USB interface:

                          

class interface implementation syntax:

class  class name extends parent class name Implements interface 1, interface 2, ... {

// class member

}

 

2. interface: A class can have only one direct parent class, but can implement multiple interfaces through implements. when a class implements multiple interfaces while inheriting the parent class, the extends keyword must precede the implements keyword;

Encoding implementation

eg : Security Door implementation process:

An interface represents a capability: A method that is embodied in an interface

Interface-Oriented Programming:

An interface is a convention that manifests itself in interface names and annotations;

interface-oriented programming: interface-oriented conventions in programming without regard to implementation;

3. define the interface syntax in C #:

Syntax:

 

 

implementation syntax:

 

Eg : Define interface:

 

 

Define implementation class:

 

 

Note: The

interface can be implemented by ":" To achieve the inheritance relationship.  An interface can inherit multiple interfaces, but an interface cannot inherit a class; the

class can inherit only one parent class, but it is possible to implement multiple interfaces and use ":" to inherit classes and implement interfaces;

An interface defines 0 or more members, mainly methods, properties, and indexers. The interface cannot contain constants, variables, and construction methods, nor can it contain any static members; public when the member access modifier is in the

interface, it is illegal to display the specified arbitrary modifier when defining an interface;

by convention, the interface in C # begins with the capital letter "I";

4. JAVA interface compared to C # interface:

Java

C#

Defined

Interface

Interface

Realize

Implements

:

Static members

Constant

No

Public

can display the use

Cannot display the use

5. abstract class and interface application methods:

Abstract class

The field cannot be abstracted (because the field can only be assigned with an equal sign, no other code (unlike attributes), no abstract meaning);

cannot be abstract to private;

Can contain non-abstract members;

cannot be instantiated;

Can only inherit from one class;

Can be inherited by an abstract class without having to implement abstract members;

Can be inherited by the ordinary class, at this time must implement the abstract members (some of the online do not have to implement, in fact, there is a mistake, the ordinary class inherits from the abstract class, must implement the abstract class of all abstract members);

Add override when implementing abstract members;

Interface:

Cannot interface to the field (because the field can only be assigned with an equal sign, no other code (different from the attribute), no meaning of the interface);

The interface can only be public and cannot use the public keyword;

In addition to the interface members, there can be no other implementation code;

cannot be instantiated;

Can inherit from multiple interfaces;

Interfaces can be inherited by interfaces, and interface members do not have to be implemented at this time;

Can be inherited by an abstract class, at which point the interface member must be implemented;

Can be inherited by ordinary class, at this time must implement interface members;

The implementation interface cannot be added override and must be public.

7. Additional:

Java inheritance is a single inheritance, that is, a class can have at most one parent class, This single-inheritance mechanism guarantees the purity of the class, which is simpler than the multi-inheritance mechanism in C + +. However, there is no denying that the extension of the function of sub-class has certain effect. So:

1. implementing an interface can be seen as a complement to inheritance. ( inheritance is hierarchical and less flexible.) Modifying a class breaks the balance of inheritance, and the interface has no such trouble because it only works on classes that implement the interface .

2 , the implementation of the interface can not break the inheritance relationship under the premise of a class function extension, very flexible.

An interface is an abstract class, and the method in an interface is a method that has the public access modifier, and the attribute in the interface is a static constant;

The method in the interface defaults to public abstract, even if it is not written. The methods in the interface cannot be modified by static.

  • Class is the definition of classes, extends is the inheritance of the keyword, implements is the implementation of the interface keyword, interface is the definition of the interface keyword;

Description of the interface:

An interface type implements a Class object, which is also a manifestation of polymorphism;

Interface is a special abstract class, all the methods in the interface are abstract methods, all the properties are static constants;

Interface is an object-oriented mechanism provided by Java;

interfaces can inherit interfaces;

The interface is used to reduce the coupling degree between modules;

Multiple interfaces can be implemented in a class: Java interfaces are declarations of a series of methods, a collection of method features, an interface with only method features, no method implementation,

Therefore, these methods can be implemented in different classes, and these implementations can have different behaviors (functions).

Only constants can be defined in an interface where variables are not defined: If the interface can define variables, but the methods in the interface are abstract, the properties cannot be modified by behavior in the interface;

A class implements an interface, which is a parent-child relationship between an interface and an implementation class. The subclass itself is instantiated, and the child class is instantiated and assigned to the parent interface is normal, which is the upward transformation

The common constants that are defined in the interface are global, with the modifier public, static, and final by default.

declaring interfaces:

The variables in the interface can only be static constants, and must be assigned values;

Methods must be abstract methods;

Interfaces can only be decorated with public, or omitted;

Eg:

Analysis:

the properties defined in the interface are static constants, and the constants are not modified in 4 options .

So there is no exception, so choose C;

Eg:

Analysis:

In Java general usage, if you want to implement an interface with a normal class, you must implement all the methods in that interface, which leads to the need to implement redundant methods;

Using an abstract class to implement a method, you can implement some of the methods in the interface, and when other classes inherit the abstract class, you only need to implement an abstract class in the non-implemented method.

Example: Abstract class B only implements the method A, B in interface A,

When Class C inherits Class B, you only need to implement interface C that is not implemented in B.

In general, methods A and B in Class C are called methods A, B of the parent class B.

Eg:

Eg:

Analysis:

When you call the study () method, you want to stand in the location of the study () method,

Because the Kid object is in the main method, it does not pass the age=5 as a parameter to the study () method.

Therefore, at run time, the study () method calls the age member variable under the class by default.

Interfaces cannot contain concrete methods, so interfaces cannot inherit classes (or inherit abstract classes).

Interfaces can inherit multiple smaller interfaces, which is the basis of common design ideas for interfaces:

The first subdivision of the program function, each individual small function is defined as a micro-interface, and then inherit the way, with a small interface to assemble a large multi-functional interface, convenient for different customers to achieve the specific needs of the customized interface. Distribution and exposure to the user is the use of a large multi-functional interface, a single function of the many small interfaces are reserved to themselves, because each small interface features independent and interdependent, easy to debug and maintenance.

It is important to note that abstract classes can inherit ordinary classes, and implement an abstract method based on a common class.

C # , there is the so-called "extension method" setting, which does not require inheritance and can augment the methods of an existing class.

Only static constants can be declared in an interface, and it is possible to declare a variable without the final static keyword, but the initial value must be assigned.

The method in the interface is the default or public abstract method and cannot have a method body; You can omit the abstract keyword.

The public and abstract keywords can be decorated with classes and interfaces, without the interface name ()

Final The class can be decorated but cannot be decorated with interfaces;

The difference between a Java interface and an abstract class,

Interfaces can only have public members, and abstract classes may exist public,private,protected,default members;

An interface can only have abstract methods, abstract methods and instance methods exist in an abstraction class.

An interface can only have static constants, whereas an abstract class may exist instance members;

An interface does not have a constructor method, and an abstract class exists.

Implementing object-oriented programming with Java--fourth chapter exception

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.