JAVA Basic Interface

Source: Internet
Author: User

abstract classes and abstract methods

Abstract MethodsAbstract methods in object-oriented programming languages refer to methods that have only method declarations and no concrete method bodies. Abstract methods generally exist in abstract classes or interfaces. In Java, the abstract method is toAbstractThe method of modification,only need to declare and do not need to implementAbstractvoidf ();

Abstract class

Classes that contain abstract methods are called abstract classes.

AbstractclassSample {
Private intparam1;
AbstractvoidF1 ();
voidF2 () {}
}Note:

1. If a class contains an abstract method, it must be qualified as an abstract class, or the compiler will give an error.

2, abstract class can not be instantiated, or the compiler will error.


Interfacewhat is an interfaceIn Java, an interface is aInterfaceTo decorate.

An interface can be treated as a class that is more abstract than an "abstract class".

Implementing interfaces

A class follows a specific or set of interfaces and must use the implements keyword.

InterfaceFly {
voidFly ();
}

classBirdImplementsFly {
@Override
PublicvoidFly () {
System.out.println ("Wings Fly");
}
}

Note: the method in the interface must be of the public type, and can be specified without display. However, if you specify a different type, the compiler will give an error.


Expansion interface

Friends who have played games must be familiar with this pattern.

A warrior needs to be capable of fighting. A knight needs not only combat skills but also riding.

The more advanced Paladin also needs the Mage's magical abilities. This relationship can be abstracted into the following code:

InterfaceWarrior {
voidFight ();
}

InterfaceKnightextendsWarrior {
voidRiding ();
}

InterfaceEnchanter {
voidMagic ();
}

//The Paladin class implements multiple interfaces and requires all methods that contain these interfaces
classPaladinImplementsKnight, Enchanter {
PublicvoidFight () {}
PublicvoidRiding () {}
PublicvoidMagic () {}
}


Multiple inheritance

Consider, for example, an in-service graduate student who is both a worker, a student and a graduate student. What do we do if we want to implement this inheritance relationship in our code?

Multiple inheritance is supported in C + +, butmultiple inheritance is not supported in J Ava , which is incorrect in the following ways. A class cannot extend multiple classes.

classperson {};
classStudentextendsperson {};
classPostgraduateextendsStudent {};
classEmployeeextendsperson {};
classOnjobpostgraduateextendsPostgraduate, Employee {};

Although multiple inheritance is not supported in Java, similar behavior can be achieved through an interface. A class can implement multiple interfaces.

Interfaceperson {};
InterfaceStudentextendsperson {};
InterfacePostgraduateextendsStudent {};
InterfaceEmployeeextendsperson {};
classOnjobpostgraduateImplementsPostgraduate, Employee {};

JAVA Basic Interface

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.