Java Abstraction Class (abstract class) differs from interface (INTERFACE)

Source: Internet
Author: User

Abstract class vs. interface comparison

Abstract classes, like interfaces, cannot be instantiated and may contain methods that do not require implementation or have been implemented.

Abstract classes can define a number of fields that are not static or constant, and define the specific methods for public, protected, and private access levels.

All the fields of an interface are automatically public, static, constant, and the access level of all defined methods is public.

A class can inherit only one abstract class and implement multiple interfaces.

Abstract class usage Scenarios

1. You want to share the code in several closely related classes.

2, you want to inherit your class of abstract class have some common fields or methods, or want to set protected, private access level.

3. You want to declare a field that is non-static or very volume. This allows you to define methods for accessing or modifying the state of a field.

Interface Usage Scenarios

1. You want unrelated classes to implement your interface.

2, only want to declare the behavior of a specific data type, do not pay attention to the implementation of the situation.

3, achieve multi-inheritance effect.

Example

An example of a abstract class in the JDK are Abstractmap, which is part of the collections Framework. Its subclasses (which include HashMap, TreeMap, and Concurrenthashmap) share many methods (including get, put, IsEmpty, CO Ntainskey, and Containsvalue) that Abstractmap defines.

An example of a class in the JDK this implements several interfaces is HashMap, which implements the interfaces Serializab Le, cloneable, and map<k, V> By reading this list of interfaces, you can infer a instance of HashMap (regardless of the developer or company who Implemented the class) can be cloned, was serializable (which means that it can be converted into a byte stream; see the SE Ctionserializable Objects), and has the functionality of a map. In addition, the map<k, v> interface have been enhanced with many default methods such as merge and ForEach that Olde R classes that has implemented this interface does not has to define.

Note that many software libraries use both abstract classes and interfaces; The HashMap class implements several interfaces and also extends the abstract class Abstractmap.

Can the interface have an implementation method?

JAVA8 allows interfaces to have default methods and static methods, except that they are called differently, as follows.

 Public InterfaceRdnum {voidPlay (); Static intGetanum () {return123; }        defaultstring getastirng (String str) {returnSTR + "Boingonium hindering"; }} Public classRImplementsInterface {...} Public classTest { Public Static voidMain (string[] args) {R R=NewR (); System.out.println (R.getastirng ("Hahaha"));    System.out.println (Rdnum.getanum ()); } }

Reference documents

Https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Java Abstraction Class (abstract class) differs from interface (INTERFACE)

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.