Interface and abstract class)

Source: Internet
Author: User

In the concept of object-oriented, we know that all objects are depicted through classes, but not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains. They are abstractions of a series of seemingly different but essentially identical specific concepts, we cannot instantiate them (we can't get a specific thing) So it is called abstraction.

In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions, but this group of actions can have any specific implementation method. This abstract description is an abstract class, and any specific implementation of this group may be represented by all the derived classes of this abstract class.

All abstract methods in interfaces and abstract classes cannot be implemented in detail. Instead, all abstract methods should be implemented in their subclasses (function bodies should be available, even if {} is empty ), java designers may consider the flexibility of abstract methods. each subclass can implement abstract methods as needed.

// The abstract class must be instantiated to call the method abstract class in the abstract class. The definition is as follows: public abstract class implements actclass // there must be at least one abstract method {public int T; // common data member public abstract void Method1 (); // abstract method, the abstract class subclass must implement the abstract method public abstract void method2 () in the abstract class (); public void method3 (); // non-abstract method public int method4 (); publi int method4 (){...... // The default behavior that can be granted to non-abstract method methods in the abstract class, that is, the specific implementation of the method} public void method3 (){...... // The default behavior that can be granted to non-abstract method methods in the abstract class, that is, the specific implementation of the method} interface is defined as follows: public interface {static final int I; // The interface cannot contain common data members. It can only have static data members that cannot be modified. Static indicates global, final indicates unchangeable, and static final does not need to be modified, it is implicitly declared as static and final public void Method1 (); // methods in the interface must be abstract methods, so public void method2 () is not required to be modified in abstract (); // The interface cannot assign the default behavior of the method, that is, there cannot be a specific implementation of the method}


In short, an abstract class is a class with incomplete functions. An interface is only a set of abstract method statements and static data that cannot be modified. Neither of them can be instantiated.

Abstract class and interface are two mechanisms supported for the definition of abstract classes in Java. It is precisely because of the existence of these two mechanisms that give Java powerful object-oriented capabilities.

Abstract class and interface have great similarity in support for the definition of abstract classes, and can even be replaced with each other,

In a sense, interface is a special form of abstract class.

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.