Java Fundamentals Hardening 18: Abstract classes, interface differences, and abstract classes can not implement all the methods of the interface

Source: Internet
Author: User

The difference between an abstract class and an interface

Abstract classes can have non-abstract methods, interfaces can only have abstract methods.

Abstract method declarations in abstract classes cannot have curly braces, and all methods in an interface do not have curly braces.

Abstract class:

A. Abstract classes are for derived classes only, cannot instantiate an abstract class, and can only instantiate classes derived from it.

B. The main feature of an abstract class is that it contains abstract member, an abstract member is a method or property that does not have an implementation, and its purpose is to force all derived classes to provide the implementation.

C. Since abstract members should be overridden (override), such members will automatically become virtual members and cannot be declared as such.

D. Abstract members cannot be private, otherwise derived classes cannot see them.


Interface (interface):

A. An interface is a variant of an abstract class.

B. A key feature of the interface is that it neither contains the implementation nor contains data.

C. Fields (that is, data) cannot appear in an interface, and if an interface requires a derived class to contain specific data, it uses properties instead of fields. Because the property does not contain any implementations as part of the interface declaration, he does not reference a support field.

D. The purpose of the interface is to define a contract that is adhered to by multiple classes, so all members of the interface must be of the public type.

Abstract classes can not implement all the methods of the interface

Sometimes it is necessary to use the interface and abstract classes together, so that developers can provide a considerable convenience, developers think which convenient to choose which. Such an abstract class is called a convenience class. At this point, the convenience class does not need to implement all the methods of the interface and can be left to the subclasses that inherit it to implement them.

This is not meaningless, when you write your own class to use the interface of the individual methods (note not all methods), then you can use an abstract class to implement this interface (the method body is empty), and then use your class to inherit the abstract class, so that you can achieve your purpose, if you directly use the class to implement the interface , that's what all the methods have to achieve.

E.G1:

InterfaceITest {voidtest1 (); voidtest2 ();} Public Abstract classTestImplementsITest {//abstract classes do not all implement all the methods in the interface          Public voidtest1 () {System.out.println ("test1!! "); } }classTestchildextendsTest {//The above interface ITest The remaining method Test2, in the abstract class Test subclass Testchild to implement.            Public voidtest2 () {System.out.println ("test2!! "); } }

E.G2:

InterfaceITest {voidtest1 (); voidtest2 (); voidtest3 ();} Public Abstract classTestImplementsITest {//Abstract classes Implement interface methods that you do not want to implement, and the implementation body is empty          Public voidtest1 () {} Public voidtest2 () {}}classTestchildextendsTest {//subclass of abstract class test to implement the interface method we want to implement           Public voidtest3 () {System.out.println ("test3!! "); } }

Java Fundamentals Hardening 18: Abstract classes, interface differences, and abstract classes can not implement all the methods of the interface

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.