Java interface implementation and inheritance

Source: Internet
Author: User
Tags define abstract

I. Abstract class

Sometimes, we may want to construct a very abstract parent class object. It may only represent a classification or abstract concept, and its instance has no meaning, so we do not want it to be instantiated. For example, there is a parent class"
Fruit (fruit), which has several sub-classes: Apple, orange, and banana. Here the fruit is just used as a classification,
Obviously, the fruit instance has no significance (just like if a person tells you that he has bought some fruit but does not tell you whether it is an apple or an orange, it is hard to imagine what he actually bought .). Fruit must be able to get quilt
Class, which requires that we use abstract class to solve this problem.

In Java, a class can be defined as an abstract class by adding the abstract modifier before the class keyword. Abstract classes cannot be instantiated. For example:

Define abstract class fruit (fruit)

Public abstract class fruit {

......

}

If we try to use the following statement to obtain an instance, the compilation will fail.

Fruit fruit = new fruit ();

We can still construct sub-classes of the fruit class, such:

Subclass "apple )"

Public class Apple extends fruit {

......

}

Subclass "orange )"

Public class orange extends fruit {

......

}

So that we can achieve our goal.

Abstract classes can have the same attributes and methods as normal classes, and abstract methods ). For example:

Abstract class shape has the abstract method draw ().

Public abstract class shape {

......

Public abstract void draw ();

......

}

Abstract METHODS correspond to abstract behaviors. Generally, this behavior has no meaning to the parent object, while sub-objects have specific actions. For example, the method draw () has no significance for class shape, while the class
The rectangle () method of the Shape subclass can have actual actions (draw the four sides of the rectangle based on the four vertices of the rectangle) and the method of the circle of the subclass.
Draw () can also have actual actions (draw a circle Based on the center and radius ).

Abstract classes can have abstract methods or no abstract methods. However, if a class has abstract methods, this class can only be defined as an abstract class.

If the following code class "shape" still has the abstract method draw () but is not defined as an abstract class, compilation will fail.

Public class shape {

......

Public abstract void draw ();

......

}

Another feature of an abstract method is that it forces the subclass to either remain abstract (that is, it does not implement the method and is still defined as an abstract class ), you can either display the behavior of this method (implement the specific action or throw an unsupportedoperationexception to indicate that this action is not supported ). This also strengthens polymorphism.

 

 

 

Second Interface

 

Next we will talk about interfaces ). Java uses the keyword interface to define an interface. An interface is an abstract object, which is even more abstract than an abstract class. All methods in the interface are abstract methods.

An interface can inherit other interfaces. A Class declares an interface through the implements keyword and implements the interface method.

For example, there is an interface interfacea,

Java code

Public interface interfacea {

Void methoda ();

}

 

Class classa implementation interface interfacea.

Java code

Public class classa implements interfacea {

Public void methoda (){

System. Out. println ("methoda of classa implements interfacea ");

}

}

 

If an abstract class implements an interface, it can be implemented by its subclass instead of the method that implements the interface (to keep its abstraction.

Abstract class classb implements interface interfacea, but there is no specific implementation method methoda (),

Java code

Public abstract class classbs implements interfacea {}

 

Subclass classbsub implements interface interfacea, but there is no specific implementation method methoda (),

Java code

Public class classbsub implements interfacea {

Public void methoda (){

System. Out. println ("methoda of classbsub The subclass of classb ");

}

}

 

An obvious commonality between an interface and an abstract class is that both interfaces and abstract classes can have abstract methods.

The differences between interfaces and abstract classes are as follows:

(1) abstract classes can have instance variables, while interfaces cannot have instance variables. All variables in interfaces are static constants (final ).

(2) abstract classes can have non-abstract methods, while interfaces can only have abstract methods.

 

Java allows an interface to inherit multiple parent interfaces, and also allows a class to implement multiple interfaces. Such multi-inheritance has the disadvantages mentioned above?

The answer is no. This is determined by the abstraction of the interface.

As described above, there cannot be instance variables in the interface, but only static constants, no specific methods (including method bodies), and only abstract methods, therefore, the disadvantages of Multi-inheritance are abandoned.

When a class implements multiple interfaces, because the interface only has abstract methods, the specific methods can only be implemented by the class that implements the interface, during the call, only methods of the implementation class will be called (there is no ambiguity ),
Therefore, there is no second disadvantage of Multi-inheritance. The interface only has static constants, but static variables determine the call relationship during the compilation period, even if a conflict exists, an error is prompted during compilation.
Static variables generally use class names or interface names directly to avoid ambiguity. Therefore, there is no first disadvantage of Multi-inheritance.

These disadvantages do not exist when an interface inherits multiple parent interfaces.

See the following example.

Interface:

Java code

Public interface interfacea {

Int Len = 1;

Void output ();

}

 

Interface B:

Java code

Public interface interfaceb {

Int Len = 2;

Void output ();

}

 

Interface interfacesub inherits interfaces A and B:

Java code

Public interface interfacesub extends interfacea, interfaceb {}

 

Class XYZ implementation interface interfacesub:

Java code

Public class XYZ implements interfacesub {

Public void output (){

System. Out. println ("output in class XYZ .");

}

Public void outputlen (INT type ){

Switch (type ){

Case interfacea. Len:

System. Out. println ("Len of interfacea =." + type );

Break;

Case interfaceb. Len:

System. Out. println ("Len of interfaceb =." + type );

Break;

}

}

Public static void main (string [] ARGs ){

XYZ = new XYZ ();

XYZ. Output ();

XYZ. outputlen ();

}

The above Code does not have any problems, but if you try to write the following conflicting code, the compilation will fail.

Java code

XYZ = new XYZ ();

Int Len = xyz. Len;

System. Out. println (LEN );

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.