interfaces and inner classes provide us with a more structured approach to separating interfaces from implementations.
Abstract classes and interfaces are two mechanisms for defining abstract concepts in the Java language, and it is their presence that gives Java a powerful object-oriented capability. The support for abstract concepts between them is very similar, even interchangeable, but there are differences.
one, abstract class
We all know that in an object-oriented domain everything is an object, and all objects are described by classes, but not all classes describe objects. If a class does not have enough information to describe a specific object, and other specific classes are required to support it, then such a class is called an abstract class. For example, New Animal (), we all know that this is the creation of an animal Animal object, but this Animal exactly what it looks like we do not know, it does not have a specific concept of animals, so he is an abstract class, need a specific animal, such as dogs, cats to make a specific description of it , we know what it's like.
Abstract classes that represent abstract concepts cannot be instantiated because abstract concepts do not correspond to specific concepts in the domain of problems in the object-oriented domain.
At the same time, abstract class embodies the idea of data abstraction, which is a mechanism to realize polymorphism. It defines a set of abstract methods, and the concrete representations of this set of abstract methods are implemented by derived classes. At the same time, abstract classes provide the concept of inheritance, and its starting point is to inherit, otherwise it does not exist any meaning. Therefore, the definition of the abstract class must be used to inherit, while in an abstract class as a node in the hierarchy chain of inheritance, the leaf node must be a specific implementation class. (I wonder if it's wrong to understand!!! Expert advice ...)
There are a few things to note when using abstract classes:
1, the abstract class can not be instantiated, the instantiation of the work should be to its subclasses to complete, it only needs to have a reference.
2. Abstract methods must be overridden by subclasses.
3, as long as an abstract class that contains an abstract method, the method must be defined as an abstract class, whether or not there are other methods included.
4, the abstract class can contain specific methods, of course, can not include abstract methods.
5. An abstract method in a subclass cannot have the same name as an abstract method of a parent class.
6. Abstract cannot modify the same class as final.
7. Abstract cannot modify the same method with private, static, final or native. 、
Example:
Define an abstract animal class animal, provide abstract methods called Cry (), cats, dogs are subclasses of animal classes, because cry () is an abstract method, so cat, dog must implement the Cry () method. As follows:
[Java] View plain copy print? public abstract class animal { public abstract void cry (); } Public class cat extends animal { @Override public void cry () { system.out.println ("Cat: Meow ..."); } } public class dog extends animal{ @Override public void cry () { System.out.println ("Barking: Barking ..."); } } public class test { public static void main (String[] args) { animal a1 = new cat (); animal a2 = new dog (); a1.cry (); a2.cry (); } } -------------------------------------------------------------------- Output: cat bark: Meow ... Dog Bark ...
It is useful to create abstract classes and abstract methods because they can make the abstraction of the class clear and tell the user and the compiler how they intend to use them. Abstract classes are also useful refactorings because they allow us to easily move the public approach up the inheritance hierarchy. (From:think in Java)
Second, interface
An interface is a "class" that is more abstract than an abstract class. I can't find a better word for "class" here, but let's be clear that the interface itself is not a class, as can be seen from the fact that we cannot instantiate an interface. If the new Runnable () is definitely wrong, we can only new its implementation class.
Interfaces are used to establish a protocol between classes, and it provides only a form, not a concrete implementation. The implementation class that implements the interface must implement all the methods of the interface, and by using the Implements keyword, he indicates that the class is following a particular interface or a set of interfaces, while also indicating that "interface is just its appearance, but it now needs to declare how it works".
Interface is an extension of the abstract class, Java to ensure that data security can not be multiple inheritance, that is, inheritance can only exist a parent class, but the interface is different, a class can implement multiple interfaces, regardless of the relationship between these interfaces, so the interface makes up for the abstract class can not be multiple inheritance defects However, it is recommended that inheritance and interfaces be used together, as this guarantees both data security and multiple inheritance.
There are several issues to be aware of when using interfaces:
1, a interface party all the legal access rights are declared as public automatically. It's only public, of course, and you can show the declaration as protected, private, but the compilation will go wrong.