Classes and Interfaces in Java

Source: Internet
Author: User

There are two types of classes:

General class:

Abstract class:

Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.

Because an abstract class cannot instantiate an object, an abstract class must be inherited (extends) in order to be used. For this reason, it is often decided at the design stage to not design abstract classes.

The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.

In Java, an abstract class represents an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.

Abstract methods

If you want to design a class that contains a special member method whose implementation is determined by its subclasses, you can declare the method as an abstract method in the parent class.

The abstract keyword can also be used to declare an abstraction method, which contains only one method name and no method body.

Abstract methods are not defined, followed by a semicolon, not a curly brace, directly following the method name.

public abstract void show();

Abstract class Summary provisions
    • 1. Abstract class can not be instantiated, if instantiated, will be error, compilation can not pass. Only non-abstract subclasses of abstract classes can create objects.

    • 2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

    • 3. Abstract methods in abstract classes are only declarations, do not contain method bodies, that is, the concrete implementation of the method is the specific function of the method.

    • 4. A constructor method, a class method (a static decorated method) cannot be declared as an abstract method.

    • 5. Subclasses of an abstract class must give a concrete implementation of an abstract method in an abstract class, unless the subclass is also an abstract class.

Interface (interface):

Interface (English: Interface), which is an abstract type in the Java programming language, is a collection of abstract methods, and interfaces are usually declared with Interface. A class inherits the abstract method of an interface by inheriting the interface. Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.

Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.

The interface cannot be instantiated, but it can be implemented. A class that implements the (Implemets) interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class.

In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.

Inheritance of interfaces

An interface can inherit another interface, and the inheritance between classes is more similar. The inheritance of an interface uses the extends keyword, which inherits the method of the parent interface.

Tag interface

The most common inherited interface is an interface that does not contain any methods.

The identity interface is an interface that does not have any methods and properties. It merely indicates that its class belongs to a particular type, and that other code is allowed to do some things to test.

Identity interface function: The simple image is to give an object a mark (a stamp), so that the object has some or some of the privileges.

Interface Features
    • Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract(only public abstract, and other modifiers will be error-marked).
    • The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, and a compilation error is reported with the private adornment.)
    • The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.
The difference between an abstract class and an interface
    • 1. Methods in an abstract class can have a method body, which is the specific function that can implement the method, but the method in the interface is not.
    • 2. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
    • 3. Interfaces cannot contain static code blocks and static methods (methods that are decorated with static), whereas abstract classes can have static code blocks and static methods.
    • 4. A class can inherit only one abstract class, while a class may implement multiple interfaces.

Classes and Interfaces in Java

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.