Java basic 10 interface inheritance and Abstraction Class (reprint)

Source: Internet
Author: User

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 Metriccup extends cup,player{
void DisPlay ();
}
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)

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.

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.

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.