Slow down the road, learn some Jakarta basics-dig into Java interfaces and abstract classes

Source: Internet
Author: User

In the Java object-oriented programming, abstract classes and interfaces always have a question, because there are too many similarities there are too many differences, in the beginning of learning often wrong, use scenarios to confuse, today to summarize before learning Java interface and abstract class problems.

Abstract classunderstanding: Declared only, not specifically implemented.
Abstract void cry ();

  Abstract methods must be decorated with the keyword abstract, and if a class contains an abstract method, the class must be decorated with abstract, because an abstract class contains an unassigned method, an abstract class cannot be instantiated, and an abstract class does not necessarily contain an abstract method. In other words, abstract classes can have no abstract method (then why should this abstract class be designed?) Abstract methods cannot exist method bodies; Abstract classes can have implemented methods when an inherited parent class is an abstract class, the abstract method of the abstract class must be fully implemented.

For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) Abstract classes cannot be used to create objects;

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

  Interfacedesign idea: cohesion-poly low coupling
[publicinterface  InterfaceName {   }

  Use the Implement keyword to follow the interface:
Class

When a class implements multiple interfaces, it is necessary to implement all the methods of the interface if such things are not abstract classes;principles:

When a class implements an interface, it is necessary to implement all the methods of this interface;

The interface cannot be instantiated (it is because the variable of the interface cannot be decorated with private protected, the default public static final, and the method is implicitly specified as the public abstract method with other keywords, such as private, Protected, static, final, and so on will report compilation errors))

All methods in an interface cannot have a method body, that is to say, an abstract method;

A variable defined in an interface, used as a global variable: An Access-form interface name. Variable name.

Interfaces cannot inherit classes, but can inherit other interfaces.

comparison of the two

A class can only inherit one abstract class at a single step. But a class can implement multiple interfaces .

  Member variables: Abstract classes can be of any type, and interfaces can only be public static final types.

Member methods: Abstract classes can provide a method body for implementations, and only public abstract methods can exist in an interface.

  static Method | code block: The interface does not exist, abstract classes can have.

Slow down the road, learn some Jakarta basics-dig into Java interfaces and abstract classes

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.