Java basic 10 interface inheritance and abstract class implementation interface implementation Interface

Source: Internet
Author: User

Author: 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 the interface from the class definition to form a subject. Interface providesInterface Specification.

In inheritanceProgramThe reusability of the introduced Inheritance Mechanism. At that time, the inheritance was based on classes. The interface can also be inherited to expand the original interface.

 

Interface inheritance

Interface inheritance(Inheritance) is similar to class inheritance. It adds a new interface method prototype based on the inherited interface. For example, we use cup as the original interface:

InterfaceCup {VoidAddwater (IntW );VoidDrinkwater (IntW );}

We define a newCup with scaleOfInterface, metriccup

The interface is as follows:

InterfaceMetriccupExtendsCup {IntWatercontent ();}

We added a new method prototype watercontent (), which returns an integer (volume of water ).

 

Multi-inheritance of interfaces

In Java class inheritance,A category class can have only one base class.. That is to say, a class cannot inherit more than one class at the same time. In Java, interfaces can inherit more than one interface at the same time. This is calledMultiple inheritance).

For example, we have the following player interface:

InterfacePlayer {VoidPlay ();}

 

We have added an interface for musiccup. It has both the cup interface and the player interface, and adds a display () method prototype.

 
InterfaceMusiccupExtendsCup, player {VoidDisplay ();}

 

(For details about how to use interfaces, see implementation interfaces)

 

Abstract class

In our life, we have some very abstract concepts. These abstract concepts are often a set of many classes, such:

    • Grain (can be corn, wheat, rice)
    • Graphics (triangles, circles, and squares)

For another example, we have provided an example:

    • Human (can be a man or a woman)

When organizing such a relationship, we canUse inheritanceFor example:

Based on our common sense:

    • The "Food objects" statement is abstract. Such an object should belong to one of the corn, rice, and wheat subclasses.
    • FoodsEat ()Method (food can be eaten ). However, such an action is abstract. The specific ways to eat food are different. For example, corn needs to be peeled and wheat needs to be ground into flour. We need to override the Eat () method of the food class in each class.

 

Abstraction and specifics

 

Java providesAbstract class)Is used to describe the class and its methodAbstraction. For example:

Abstract ClassFood {Public Abstract VoidEat ();
Public voidHappyfood ();
{
System. Out. println ("Good! Eat me! ");
}}

The method in the class can be declaredAbstractFor exampleEat (). In this case, we do not need to define a method. We only need to provide a prototype for this method. This is similar to the interface. When we inherit this class in the corn class for example, we need to provideEat ()Method.

Another method in the class, happyfood (), is not

When the abstract method appears in a class, the abstract keyword must be added to the declaration of this class, otherwise Java will report an error. An abstract class cannot be used to create objects.

 

Inheritance of abstract classes

We can inherit an abstract class like an inherited class. We must use the complete method definition to overwrite the abstract methods in the abstract class. Otherwise, the abstract class is still an abstract class.

Abstract classes can contain data members. The inheritance of data members is the same as that of normal classes.

 

Summary

Interface inheritance, multi-Inheritance

Abstract method, abstract class

 

Welcome to continue reading the "Java quick tutorial" SeriesArticle

 

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.