Java Basic Tutorial interface inheritance and abstract class _java

Source: Internet
Author: User
Tags class definition inheritance

In the implementation interface, we use the interface syntax to separate the interface from the class definition and form a body. Interface provides the interface specification for the class.

In inheritance, we introduce the inheritance mechanism in order to improve the reusability of the program. The inheritance at that time was based on class. The interface interface can also be inherited to extend the original interface.

Interface inheritance

Interface Inheritance (inheritance) is similar to class inheritance by adding new interface method prototypes based on inherited interface. For example, we use Cup as the original interface:

Copy Code code as follows:

Interface Cup {
void Addwater (int w);
void Drinkwater (int w);
}

On the basis of inheriting cup, we define the interface of a new, calibrated cup, Metriccup

The interface is as follows:

Copy Code code as follows:

Interface Metriccup extends Cup
{
int watercontent ();
}

We have added a new method to prototype Watercontent (), this method returns an integer (water).

Multiple inheritance of interface

In the inheritance of Java classes, 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:

Copy Code code as follows:

Interface Player
{
void Play ();
}

We have added a Musiccup interface. It has both the Cup interface and the player interface, and adds a display () method prototype.

Copy Code code as follows:

Interface Musiccup extends Cup, Player
{
void display ();
}

(How to use interface, see Implementation interface)

Abstract class

In life, we have some very abstract concepts. These abstract concepts are often collections of many classes, such as:

1. Grain (can be corn, wheat, rice)
2. Graphics (can be triangular, round, square)

Again, for example, we cited the following examples:

1. Human beings (can be men, women)

When organizing such a relationship, we can use inheritance, such as:

According to our common sense:

1. The term "object of the Food class" is abstract. Such an object should belong to one of the corn, Rice, wheat subclasses.

The 2.Food class has a eat () method (food 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 overwrite the Eat () method of the food class in each class.

Abstract and concrete

The syntax for abstract classes (abstract class) is provided in Java to illustrate the abstraction of classes and their methods. Like what:

Copy Code code as follows:

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 the Eat () above. At this point, we do not need to define the method, only to provide the prototype of the method. This is similar to the interface. When we inherit the class in a class such as corn, we need to provide a specific definition of the Eat () method.

Another method in the class Happyfood () is not

When the abstract method appears in a class, the declaration of the class must be accompanied by an abstract keyword, or Java will complain. An abstract class cannot be used to create an object.

Inheritance of abstract classes

We can inherit an abstract class like an inherited class. We must override the abstract method in the abstract class with the complete method definition, otherwise the derived class is still an abstract class.

The definition of an abstract class can have data members. The data member inherits the same inheritance as the normal class.

Summarize

Interface inheritance, multiple inheritance

Abstract method, abstract class

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.