Link Address: http://www.cnblogs.com/vamei/archive/2013/03/31/2982240.html
Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
In the implementation interface, we use the interface syntax to separate interface from the class definition to form a body. Interface provides the interface specification for the class.
In the inheritance, we introduce the inheritance mechanism in order to improve the reusability of the program. The inheritance at that time was class-based. The interface interface can also be inherited to extend the original interface.
Interface inheritance
Interface Inheritance (inheritance) is similar to class inheritance, with the addition of new interface method prototypes based on inherited interface. For example, we use Cup as the original interface:
Interface Cup { void addwater (int w); void Drinkwater (int w);}
On the basis of inheriting cups, we define the interface of a new graduated Cup, Metriccup
The interface is as follows:
Interface Metriccup extends cup{ int watercontent ();}
We have added a new method to prototype Watercontent (), which returns an integer (water).
Multiple inheritance of interface
In the Java class inheritance, a derived class can have only one base class. In other words, a class cannot inherit more than one class at a time. In Java, interface can inherit more than one interface at a time, which is called multiple inheritance (multiple inheritance).
For example, we have one of the following player interfaces:
Interface player{ void Play ();
We have added a Musiccup interface. It has both a cup interface and a player interface, and adds a display () method prototype.
Interface Musiccup extends Cup, Player { void display ();}
(How to use interface, see Implementing Interface)
Abstract class
In life, we have some very abstract concepts. These abstract concepts are often a collection of many classes, such as:
- Grain (can be corn, wheat, rice)
- Graphics (can be triangles, circles, squares)
For example, we have previously cited examples:
- Human (can be a man, a woman)
In organizing such a relationship, we can use inheritance, such as:
According to our common sense:
- The term "Food class object" is abstract. Such an object should belong to one of the corn, rice, wheat subclasses.
- Food classes have Eat () methods (foods can be eaten). However, such an action is abstract. The specific eating method of grain is different. For example corn need to peel to eat, wheat to grind into flour to eat. We need to cover the Eat () method of the food class in each class.
Abstract and concrete
The syntax of abstract class is provided in Java to illustrate the abstraction of classes and their methods. Like what:
Abstract class Food {public abstract void eat ();
public void Happyfood ();
{
System.out.println ("good! Eat me! ");
}}
A method in a class can be declared abstract, such as Eat () above. At this point, we do not need to define the method, only the prototype of the method needs to be provided. This is similar to an interface. When we inherit this class in the example corn class, we need to provide a concrete definition of the Eat () method.
Another method in the class Happyfood () is not
When an abstract method appears in a class, the declaration of the class must be prefixed with the abstract keyword, or Java will error. An abstract class cannot be used to create an object.
Inheritance of abstract classes
We can inherit an abstract class as if it were an inherited class. We must use the complete method definition to override the abstract method in the abstract class, otherwise the derived class is still an abstract class.
You can have data members in the definition of an abstract class. The inheritance of the data member is the same as the inheritance of the normal class.
Summarize
Interface inheritance, multiple inheritance
Abstract method, abstract class
Welcome to the "Java Quick Tutorials" article series
Java basic 10 interface inheritance and abstract classes