Java Abstract classes and interfaces

Source: Internet
Author: User

From: http://blog.chinaunix.net/uid-25885064-id-3361989.html

Java allows an abstract method. It is just a name without specific implementation. For example, public abstract void ABC (); uses the abstract keyword and ends. Classes that contain one or more abstract methods are called abstract classes. Abstract classes must also declare abstract keywords. The use of abstract classes has some restrictions, so you cannot create instances of abstract classes. If a subclass implements an abstract method, you can create an instance object for this subclass. If the subclass is not implemented, this subclass is also an abstract class and cannot be used to create instances.

What is an interface? Interfaces are more abstract classes than abstract classes. For example, the methods in the public interface name {} interface are all abstract, and all the variables in the interface are final constants. The class that implements the interface must implement all the abstract methods. Abstract classes can also have specific methods. Therefore, the interface is the most abstract, followed by the abstract class, and the specific class itself is the abstraction of the real world. Software Development itself abstracts the real world into a computer world.

Abstract classes and interfaces are more abstract than specific classes, so they are always inherited and implemented during use. But the classes that inherit them are not just one. There are many classes that implement their abstract methods. One method has multiple implementation methods. polymorphism in OOP is used here. This makes the design very clear. Because the base class is an abstract class or interface, there are several inherited classes under it. You only need to operate on the interface or abstract class, and you do not need to worry about how many implementations are there. It makes sense to develop projects jointly by multiple people.

Keyword implement instead of extends is used for interface implementation. If extends is used, this interface is inherited. That subclass is also an interface, which is the original subinterface.


  1. // Declare an Interface
  2. Interface say {
  3. Public void saymessage ();
  4. }

  5. // Two implementation classes
  6. Class sayhello implements say {
  7. Public void saymessage (){
  8. System. Out. println ("hello ");
  9. }
  10. }

  11. Class sayhi implements say {
  12. Public void saymessage (){
  13. System. Out. println ("hi ");
  14. }
  15. }

  16. Public class testsay {
  17. Public static void main (string [] ARGs ){
  18. // You can also use the say interface type instance to output two results.
  19. Say = new sayhello ();
  20. Say. saymessage ();
  21. Say say1 = new sayhi ();
  22. Say1.saymessage ();
  23. }
  24. }

The interface also plays an important role. Java only has single inheritance, that is, it can only inherit from a parent class. The benefit of single inheritance is that, once too many classes are inherited, The subclass changes. What if I want to inherit the features of multiple parent classes? Let's just use the interface. This class can inherit a class first, and then implement other interfaces. The interfaces are all abstract methods, which will not cause a systemic effect. Changing the multi-inheritance feature is also an improvement for the c ++ language.

There is a saying in the industry that Java is not so much object-oriented programming as interface-oriented programming. The emphasis is on the abstract description of interfaces. It is also an improvement for C ++, and there is no interface in C ++. Therefore, the Java language is suitable for large projects that involve multi-person teams. You can simply look at an interface. How can we implement it later.

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.