Abstract class and Interface day-11.2

Source: Internet
Author: User

It can be said that the quadrilateral has 4 edges, or more specifically, parallelogram is a special quadrilateral with parallel and equal properties to the edge, and isosceles triangle is a triangle with two sides equal, these descriptions are reasonable, but for the graphical object can not be described in specific language, he has a few sides, what is the image , no one can say that this kind is defined in Java as an abstract class.

One: abstract class

In solving the actual problem, the parent class is generally defined as an abstract class that needs to be inherited and polymorphic with this parent class. Recalling the principle of inheritance and polymorphism, the more in the inheritance tree in the upper class more abstract, such as pigeons inherit birds, birds inherit animals and so on. In a polymorphic mechanism, the parent class is not required to initialize the object, we need only the subclass object, so setting the abstract class in Java cannot instantiate the object, because the graphics class cannot abstract any particular shape, but their subclasses can.

Syntax for abstract classes:

Public abstract class test{

abstract void Testabstract ();//define Abstraction method

}

Where abstract is the keyword that defines a class of abstraction.

The class defined with the abstract keyword is called an abstract class, and the method defined by the keyword is called an abstract method, and the method itself has no meaning unless it is overridden and the abstract class that hosts the abstract method must be inherited. In fact, abstract classes have no meaning other than being inherited.

Conversely, if you declare an abstract method, you must define the class that inherits the abstract method as an abstract class, and it is not possible to obtain an abstract method in a non-abstract class. In other words, this class is marked as an abstract class as long as there is an abstract method in the class.

Abstract classes are inherited and need to implement all of these abstract methods, that is, to ensure that the same method name, parameter list and the same return value type to create a non-abstract method, of course, can also be an abstract method.

Inheriting all subclasses of an abstract class requires overriding the abstract methods in the abstract class. In this way, the parent class can be modified into an abstract class in a polymorphic mechanism, and the method is modified to an abstract method, and then each subclass overrides the method class processing. But this is going to happen. The polymorphism we just discussed is a matter of discussion, there are too many redundant code in the program, and the parent class is very limited, and perhaps a subclass that does not need a parent class method has to override the method. If you place the method in another class, so that the class that requires the method inherits the class, and the class that does not need the method inherits the graphics class, but all subclasses require the graphics class, because the classes are exported from the graphics class, and some classes require methods, but in Java, the class cannot inherit more than one parent class at a time. Faced with this problem, the concept of an interface arises.

Two: interface

1. Introduction to Interfaces

An interface is an extension of an abstract class that can be seen as a purely abstract class, with all methods in the interface having no method body. For the above problem, the method can be encapsulated into an interface, so that the class needs to implement this interface, and also inherit the graphics class, which is the necessity of the interface.

The interface is defined using the interface keyword with the following syntax:

public interface drawtest{

void Draw ();//method within the interface, omitting the abstract keyword

}

*public: An interface can be decorated like a class by a permission modifier, but the public keyword is only defined in a file with the same name as the interface.

*interface: Defines the interface keyword.

*drawtest: Interface name.

A class implements an interface that can use the Implements keyword, with the following code:

public class S extends Y implements drawtest{

········

}

Note: The method defined in the interface must be defined as public or abstract, and the other modifier permissions are not recognized by the Java compiler, even if the method is not declared in my public form.

Description: Any fields defined in the interface are automatically static and final.

2. Interface and inheritance

We know that multiple inheritance is not allowed in Java, but multiple inheritance can be implemented using interfaces, because a class can implement multiple interfaces at the same time, so that all interfaces that need to be inherited are placed behind the Implements keyword and separated by commas, but this can result in a huge amount of code in a class. Because you need to implement all the methods in the interface when inheriting an interface.

The syntax for multiple inheritance is as follows:

Class name implements interface 1, Interface 2, interface 3, interface n

When you define an interface, you can inherit another interface from the interface:

Interface f1{

}

Interface F2 extends f1{

}

Abstract class and Interface day-11.2

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.