Abstract classes and Interfaces in Java

Source: Internet
Author: User

Interface

1 because Java does not support multiple inheritance, there is an interface, a class can inherit only one parent class, but can implement multiple interfaces, the interface itself can inherit multiple interfaces.

The member variables in the 2 interface are public static final type by default. The initialization that must be displayed.

The method in the 3 interface is the public abstract type by default. An implicit declaration.

The 4 interface has no constructor method and cannot be instantiated.

The 5 interface cannot implement another interface, but it can inherit multiple interfaces.

Class 6 If you implement an interface, you must implement all the abstract methods inside the interface, otherwise the class will be defined as an abstract class.

Abstract class

1 If you declare a class as abstract, this class cannot generate objects and can only be used by inheritance.

2 The abstract method must exist in an abstract class.

3 There can be general variables and general methods in the abstract class.

4 Subclass inheriting an abstract class must implement an abstract method, unless the subclass is an abstract class.
private void Print () {}; This statement represents a null implementation of the method.
abstract void print (); This statement represents the abstraction of the method, without implementation.

The difference between an interface and an abstract class

The 1 interface can contain only abstract methods, and abstract classes can contain common methods.
The 2 interface can only define static constant properties, and abstract classes can define either normal properties or static constant properties.
The 3 interface does not contain a constructor method, and an abstract class can contain a constructor method.

An abstract class cannot be instantiated, but it does not mean that it can have constructors, that abstract classes can have constructors, and that they inherit class extensions

Abstract 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.