The difference between abstract classes and interfaces in Java

Source: Internet
Author: User
Tags abstract

There are a lot of similarities and similarities between interfaces and abstract classes, and many people have the question of why there must be interfaces instead of abstract classes, and in others, abstract classes rather than interfaces. Or, when considering the generalization of Java classes, many people hesitate between interfaces and abstract classes, Or even choose a random one.

Understanding Abstract Classes

Abstract class and interface are all used in the Java language for abstract classes (the abstract classes in this article are not translated from abstract class, which represents an abstract body, and abstract Class is a method for defining abstract classes in the Java language, which readers should be aware of to differentiate, so what is an abstract class, and what benefits can be derived from using abstract classes?

Abstract class: Abstract class is a special base class, which contains some basic properties and methods, but also contains some only declared but not implemented methods, in abstract to declare

For example:

Interface: interface is a more abstract type, inside the declared method all open to the implementation to implement, do not care about the specific details of the keyword is interface

The main differences between abstract classes and interfaces can be seen in the overview:

1. Abstract classes can have their own methods, but the interface method is only declared

2 abstract method of abstract class using abstract declaration

Other notes to be noted are:

1. A class inherits only one implementation class, but can implement multiple interfaces

2. When an abstract class and an interface have public properties, the compilation does not pass if a class implements both the abstract class and the interface.

Interface:

A highly abstract "abstract class" that uses the interface keyword declaration

The following defines a USB interface:

Public interface USB {

public void start ();

public void end ();

}

No construction method in interface

interfaces, like abstract classes, cannot be used for instantiating

You cannot define a variable in an interface without a method body, the method defaults to public, and the methods and abstract methods in the interface are not allowed to have private adornments.

Only interfaces can be inherited, multiple inheritance supported (separated by commas between interfaces)

class implements the interface using the keyword implements:

1. All methods of this interface must be implemented unless the implementation class is an abstract class

2. A class can implement multiple interfaces

Related Article

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.