search
Home类库下载java类库Java inheritance and interfaces

Java inheritance and interfaces

Nov 03, 2016 pm 02:47 PM
java

The most critical role of interfaces is also the most important reason for using interfaces: they can be traced back to multiple basic classes. The second reason for using an interface is the same as for using an abstract base class: to prevent the client programmer from making an object of this class and specifying that it is just an interface. This brings up a question: Should I use an interface or an abstract class? If we use interfaces, we can get the benefits of abstract classes and interfaces at the same time. So if the base class you want to create does not have any method definitions or member variables, then you are willing to use an interface instead of an abstract class anyway. In fact, if you know in advance that something is going to be a base class, your first option is to turn it into an interface. Abstract classes should be considered only when method definitions or member variables must be used.

/**
 * Created by xfyou on 2016/11/3.
 * Java 继承和接口演示
 */
public class Adventure {
    static void t(CanFight x) {
        x.fight();
    }

    static void u(CanSwim x) {
        x.swim();
    }

    static void v(CanFly x) {
        x.fly();
    }

    static void w(ActionCharacter x) {
        x.fight();
    }

    public static void main(String[] args) {
        Hero i = new Hero();
        t(i);
        u(i);
        v(i);
        w(i);
    }
}

interface CanFight {
    void fight();
}

interface CanSwim {
    void swim();
}

interface CanFly {
    void fly();
}

class ActionCharacter {
    // 父类中实现了子类中继承的接口方法
    public void fight() {
    }
}

/**
 * 必须先 extends 然后再 impplements
 */
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
    @Override
    public void fly() {

    }

    @Override
    public void swim() {

    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment