Java abstract class

Source: Internet
Author: User

Java:
1, ordinary Java class can be added to the class before the abstract declaration as an abstraction, just at this time the class can no longer be instantiated.


2. Suppose that there is more than one abstract method in a class. Then the class must be declared as an abstract class. The method must also be declared abstract.


3. Abstract classes cannot be instantiated. However, it does not mean that it cannot have constructors. Abstract classes can have constructors, and an abstract class is a class that is augmented with abstract classes .

An abstract class is incomplete, and it can only be used as a base class. It differs from non-abstract classes: 1. Abstract classes cannot be instantiated directly. and using the new operator for an abstract class is a compile-time error. Although some variables and values at compile-time types can be abstract. However, this variable and value must either be null or contain a reference to an instance of a non-abstract class (This non-abstract class derives from an abstract class). 2, agree (but do not require) abstract classes include abstract members. 3, abstract class can not be sealed.

When Congo abstract classes derive from non-abstract classes, these non-abstract classes must implement all the abstract members that are inherited in detail. Which abstract members are overridden thereby. In the Demo sample below: abstract class a{public abstract void F ();}    abstract class b:a{public void G () {}}      class c:b{public override void F () {//actual implementation of F}}    abstract class A introduces an abstract method F. Class B is introduced with a method G, but because it does not provide an implementation of F, B must also be declared as an abstract class. Class C overrides F, and provides a detailed implementation. Because there are no abstract members in C, it is possible (but not necessarily) to declare C as a non-abstract class. abstract classes are closely related to interfaces.

Then the interface is more abstract than the abstract class, the main body is now their difference: 1) class can implement an infinite number of interfaces, but only from an abstraction (or whatever other type) class can inherit. Classes derived from abstract classes can still implement interfaces. The result is that the interface is used to solve multiple inheritance problems. 2) abstract classes where non-abstract methods can exist. The interface cannot and the method inside it is simply a declaration that must be modified with public to the method without detailed implementation. 3) member variables in an abstract class can be modified by different modifiers. The member variables in the interface can default to static constants (static final). 4) This is also the most important point of the essence of the "abstract class is an abstraction of an object. Interface is a code of conduct ". The above is their own similarities and differences, the following again from the actual application of their similarities and differences!

The difference:definitionabstract classes represent a detailed definition of methods that may already exist in the class, but interfaces can only define the interface (method name, list of parameters) for each method. return type), and does not care for detailed details. How to use1) When inheriting an abstract class, you must override each of the abstract methods in the class. Each implemented method must receive the same number and type of parameters as the method specified in the abstract class. Have the same return value. This is the same as the interface.

2) when the parent class already has a method that is actually functional, the method can not be implemented in the subclass, the method directly referenced, and the subclass can override the method of the parent class (the concept of inheritance).

3) while implementing (implement) an interface (interface). Be sure to implement all the methods defined in the interface, and not to omit any one.

4) In addition, abstract classes cannot produce objects. However, the object can be declared by its implementation class.

In view of this, when implementing an interface. We often write an abstract class, to implement the interface required by some subclass common methods, when following the preparation of different subclasses, you can inherit the abstract class use, in eliminating the trouble, must achieve each common processing.

Java abstract class

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.