Differences between abstract classes and interfaces in Java

Source: Internet
Author: User
Tags define abstract

Interface:
First, declare an interface. Keyword: interface. Syntax:
Public interface interfacedemo
{
....
}
Some constants and interface declarations can be defined within the subject! For example:
Public interface interfacedemo
{
Public static int A = 14;

Public void run (int x );
}
Note that all methods stated in the interface must be public or default type!
And as long as you declare it, do not implement it!

If you want to use this interface, you need to write a class to implement it. Keyword: Implements

Public class Zi implements interfacedemo {
.......
}

Note: To implement an interface, you must implement all the methods defined in the interface, unless your class declaration is abstract (an abstract class )!

A class can implement multiple interfaces. The syntax is to separate the interfaces to be implemented with commas!
Public class Zi implements interfacedemo1, interfacedemo2 ,....
{
....
}

Abstract:
Abstract modifiers can be used for classes, methods, attributes, events, and index indicators (Indexer ),
It indicates that it is an abstract member, and abstract cannot be used together with static and virtual. Declarations for abstract members may not include implementations.Code,
However, as long as there are still unimplemented abstract members (Abstract METHODS) in the class, this class is an abstract class, and the object of the abstract class cannot be instantiated,
It is usually used to force the inheritance class to implement a member. The abstract class must be a subclass. In addition, if the subclass does not implement all methods of the abstract base class,
Then the subclass is also an abstract class.

Public abstract class abstractclass
{
Public void test1 () {}// a normal method
Public abstract void Test2 (); // an abstract method.
}
If any method of the Java class is abstract, the class itself must be abstract.

abstract class and interface:
1, abstract classes and interfaces both represent the abstract layers of the system.
2. abstract classes and interfaces cannot be instantiated, but they can inherit or implement specific classes of abstract classes or interfaces, constructed by defining reference variables.
3. abstract classes and interfaces contain abstract methods.
4. Some methods can be implemented by default in abstract classes, you can avoid repeated implementation of them in subclasses to improve code reusability. interfaces can only contain abstract methods.
5. A class can only inherit one direct parent class, such as an abstract class, but multiple interfaces can be implemented.
6. The interface can be made public, so that the encapsulation of object-oriented programming is not damaged.
7. The abstract class is an extension of the parent class, that is to say, when someone defines a method in an abstract class, you can overwrite and reload it.
difference:
1. abstract classes can only be inherited by a single object, and interfaces can be implemented multiple times, avoiding the limitations of a single object.
2. abstract classes can define abstract methods and non-abstract methods. They can be used to define basic commonalities of the system. An interface can only define abstract methods. It is mainly used to expand the functions of objects.
3. An abstract class is an inheritance relation, an is a relation, and an interface is a like a relation.
4. The member modifiers in the abstract class are all custom and the modifiers in the interface are fixed.
hope to help you !!!

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.