What is the difference between abstract classes and interfaces in Java

Source: Internet
Author: User

Abstract class: class containing the abstract modifier. It cannot create an instance object; A class containing an abstract method must be defined as an abstract class;abstract class, and the methods in the abstract class class must be implemented in a specific subclass, so There can be no abstract constructor or abstract static method, and if the subclass does not implement all the abstract methods in the abstract parent class, the subclass must also be defined as an abstract type.

interface (interface): Can be said to be a special case of abstract classes, modified by interface. All methods in an interface must be abstract; The method definition in the interface defaults to the public abstract type; the member variable type in the interface defaults to public static final.


The grammatical differences between the two:
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. There can be ordinary member variables in an abstract class, and there are no ordinary member variables in the interface.
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. Abstract methods in abstract classes can have access types of public,protected and (the default type, although Eclipse does not give an error, but should not), but the abstract methods in the interface can only be of the public type, and the default is the public abstract type.
5. Abstract classes can contain static methods, and interfaces cannot contain static methods.
6. Both abstract classes and interfaces can contain static member variables, and the access type of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public static final type is the default.

7. A class can implement multiple interfaces, but can inherit only one abstract class.

The difference between the two applications:

1. One of the biggest differences between Java interfaces and Java abstract classes is that Java abstract classes can provide partial implementations of some methods, while Java interfaces cannot (that is, only methods can be defined in interface, but not the implementation of methods, but in the abstract Class can have both the specific implementation of the method, and there is no concrete implementation of the abstract method, which is probably the only advantage of Java abstract class, but this advantage is very useful. If you add a new concrete method to an abstract class, all of its subclasses get the new method all of a sudden, and the Java interface does not do this, and if a new method is added to a Java interface, all classes that implement the interface will fail to compile successfully. Because you have to let every class implement this method again, this is obviously a disadvantage of the Java interface. This is a similar issue in one of my other blog MapReduce new and old API differences, where the new MapReduce API tends to use abstract classes rather than interfaces because it's easier to extend. The reason is that the underlined part says.

2, the implementation of an abstract class can only be given by the subclass of this abstract class, that is, the implementation of the abstract class defined by the hierarchy of inheritance, and because of the Java language single inheritance, so the abstract class as a type definition tool performance is greatly compromised. At this point, the advantages of the Java interface come out that any class that implements a method specified by a Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so there are many types of this class. (with abstract classes, the subclass type that inherits this abstract class is relatively single, because subclasses can only inherit abstract classes, whereas subclasses can implement multiple interfaces at the same time because the types are more numerous.) Interfaces and abstract classes can define objects, but only their specific implementation classes are instantiated. )

3. An interface is a set of rules that specifies a set of rules that a class or interface that implements this interface must have. Embodies the nature "if you are ... You must be able to ... "the idea.
For example, in nature, people can eat, that is, "If you are human, you must be able to eat". So the simulation to the computer program, there should be a IPerson (the custom interface name from the "I") interface, and there is a method called Eat (), and then we stipulate that each class that represents "human", must implement the IPerson interface, which simulates the nature "If you are human, you must be able to eat" This rule.

From here, I think you can see a little bit of object-oriented thinking. One of the core of object-oriented thinking is to simulate the real world and abstract the real world into classes, and the whole program relies on each kind of instance to communicate with each other and to cooperate with each other to complete the system function, which is very accord with the real world running condition and the essence of object oriented thought.

4, interface is an abstract representation of similar things on a certain granularity view. Note here I emphasize that in a certain granularity view, because the concept of "homogeneous thing" is relative, it differs because of the granularity view.
For example, in my eyes, I am a person, and a pig there is an essential difference, I can accept my classmates and I are similar this statement, but can not accept me and a pig is the same. However, if in the eyes of a zoologist, I and pig should be the same, because we are animals, he can think that "man" and "pig" have achieved ianimal this interface, and he in the study of animal behavior, I and pig will not be treated separately, but from the "animal" This larger grain size study, But he would think I was fundamentally different from a tree.
Now for a geneticist, the situation is different, because the organism can inherit, so in his eyes, I not only with the pig no difference, and a mosquito, a bacterium, a tree, a mushroom and even a SARS virus is no different, Because he would think that we all achieved idescendable this interface (note: Descend VI. Heredity), that is, we are all heritable things, he will not study us separately, but will all creatures as the same kind of research, in his eyes there is no human and virus, only the genetic material and non-hereditary material. But at least, I have a difference with a stone.
But the unfortunate thing happened, one day, on the earth appeared a great man, his name is Lenin, he is familiar with Marx, Engels of dialectical materialism thought after the masterpiece, quite experience, so he made a famous definition: the so-called material, is to be reflected in the consciousness of the objective reality. At this point, I and a stone, a trace of air, an idiom and transmission of mobile phone signal electromagnetic field has no difference, because in Lenin's eyes, we are can be reflected in the consciousness of the objective reality. If Lenin was a programmer, he would say: the so-called matter is all the instances that are generated by all classes that simultaneously implement the "Ireflectabe" and "Iesse" two interfaces. (Note: Reflect v. reflect esse N. Objective reality)
You might think that my example above is a nonsense, but that's what the interface is all about. Object-oriented thinking and one of the core is called polymorphism, what is polymorphism? To put it bluntly is to treat the same things indiscriminately at a certain granularity view level. And the reason to do this is because there is an interface exists. Like the geneticist, he knew that all living creatures had idescendable interfaces, and that, as long as they were creatures, there must be descend (), so that he could study together, rather than study each creature and end up exhausted.


Finally, when do I use interfaces and abstract classes?

See what features you want to achieve. Abstract classes are used for reuse of code, and the motivation for using interfaces is to achieve polymorphism. So, if you're hesitant about using interfaces or abstract classes somewhere, think about what your motives are.


This article is a summary of several related blog posts.



What is the difference between abstract classes and interfaces in Java

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.