Abstract class and Interface

Source: Internet
Author: User

It seems that abstract classes and interfaces are designed to achieve abstract concepts. Java Abstract classes can provide partial implementation of some methods. If a new method is added to an abstract class, then all its subclasses get this new method at once, and the Java interface cannot do this. If you add a new method to a Java interface, all classes that implement this interface cannot be compiled successfully, because you must make every class implement this method again, which is obviously a disadvantage of the Java interface.


On the Internet, the abstract class is the relationship between is-a, and the interface is the relationship between like-.


Interfaces can enrich the functions of classes. For example, a class that implements the java. Lang. Comparable interface can be compared with other objects of the same type. This can be counted as an interface advantage.

Abstract class: abstract class

1) There can be constructor in the abstract class. If no constructor is written, a default null constructor will be created. The constructor that inherits the implementation class instantiation of the abstract class will be called recursively. First, the constructor of the abstract class is called, then, call your own constructor.

/*** Class description: abstract class * @ author Wang youjie * @ version 1.0.8.1 * creation time 2014-3-4 */public abstract class myabstractclass {int I = 0; Public myabstractclass (int I) {I = 9; system. out. println ("abstract class constructor execution") ;}/ *** class description: test class * @ author Wang yaojie * @ version 1.0.8.1 * created at 2014-3-4 */public class named actttest {public static void main (string [] ARGs) {// todo auto-generated method stub ttest A = new ttest (); system. out. println (. i) ;}}/*** inherit the implementation class of the abstract class **/class ttest extends myabstractclass {public ttest () {super (1 ); // because the constructor I have given to the abstract class contains parameters, if the implementation class does not specify to call a constructor with parameters, super () is executed by default (); in this case, the error I = 10; system. out. println ("implementation class constructor execution ");}}

Output result:

Abstract class constructor execution

Implementation class constructor execution

10

2) abstract classes can have their own implementation methods. They can contain abstract methods. Abstract METHODS must contain abstract keywords and cannot be modified by private. They can be protected or public ?.


Interface:

1) All methods in the interface are of the public abstract type by default. Generally, the interfaces defined in the JDK source code omit these two keywords, and the initial values must be given, therefore, the implementation class cannot be redefined or its value cannot be changed.

2) some attributes can be defined in the interface, but these attributes are of the public static final type by default (even if these keywords are not written); ------- but the following code can modify the name attribute in the student class, because the memory address pointed by A has not changed, so OK.

/*** Class description: * @ author Wang youjie * @ version 1.0.8.1 * Creation Time: 3-4 3-4 */public interface myinterfaces extends myinterfaceg {int I = 12; // default public final static student A = new student ("Tom"); void close (string ); // The default public abstract}/*** student class **/class student {string name = ""; Public student (string name) {This. name = Name ;}public string tostring () {return name ;}}

3) the interface inheritance interface can be inherited more.

Abstract class and 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.