In Java, a class implements an interface and must override all the methods in the interface

Source: Internet
Author: User
Tags class definition modifiers

Not necessarily, the key is to see if subclasses are abstract classes.
If the subclass is a non-abstract class, all methods in the interface must be implemented;
If the subclass is an abstract class, you can not implement all the methods in the interface, because abstract methods are allowed to exist in the abstract class!
1. Abstract class definition
Abstract classes are often used to characterize the abstract concepts of analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. Classes that are typically decorated in a programming statement are abstract classes. In C + +, a class containing a purely virtual function is called an abstract class, it cannot generate an object, and in Java, a class containing an abstract method is called an abstract class, and an object cannot be generated. An abstract class is incomplete and can only be used as a base class. In an object-oriented approach, abstract classes are primarily used for type concealment and for roles that act as global variables.
2, abstract class characteristics
1) Abstract classes cannot be instantiated.
2) Abstract classes can contain abstract methods and abstract accessors.
3) You cannot use the sealed modifier to decorate an abstract class, because the meanings of the two modifiers are reversed. Classes with the sealed modifier cannot inherit, whereas the abstract modifier requires that the class be inherited.
4) A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.
3. Comparison with specific classes

1) Abstract classes cannot be instantiated directly, and using the new operator with an abstract class can cause compile-time errors. Although some variables and values at compile-time types can be abstract, such variables and values must either be null or contain references to instances of non-abstract classes (this non-abstract class derives from an abstract class).
2) allow (but not require) abstract classes to contain abstract members.
3) Abstract classes cannot be sealed.
4, compared with the interface
1) Same point

A, can not be instantiated;
b, contains the non-implemented method declaration;
C, derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members)
2) different points

A, a class can implement an infinite number of interfaces, but only from an abstract (or any other type) class can inherit, the class derived from the abstract class can still implement the interface, so that the interface is used to solve multiple inheritance problems.
B, abstract class can exist non-abstract methods, interface can not, and its method is only a declaration must be public to decorate the method without concrete implementation.
C, member variables in an abstract class can be decorated with different modifiers, and the member variables in the interface can default to static constants (static final).
Abstract A class is an abstraction of an object, whereas an interface is a code of conduct.

In Java, a class implements an interface and must override all the methods in the 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.