Java Object-oriented interface to the (b)

Java Object-oriented interface to the (b)

The foregoing Portal: the Java-oriented interface object of (a)

  • We talked about before, Java as an object-oriented language that does not support multiple inheritance. But in Java interfaces can achieve multiple inheritance will appear: interface A extends InterfaceC,InterfaceDform, to a certain extent, increase flexibility. Not only that, Java in the class can also implement (implements) multiple interfaces, indirectly achieve the inherited function, become "multi-realized" .
  • In fact, when we understand interface, you will find this is entirely reasonable. Why do you say? If you inherit two classes, two classes have the same name but different methods of behavior, you are not in a derived class does not know what is not a headache in Java. But the interface is not the same, we know, an abstract interface methods or default method will eventually be implemented in the class definition ( if two methods the same signature but different return types, then when you need to return both the implementation of common sub type ) so there is no difficulty in selection. Next, we analyze the problem slowly interface inheritance may occur.

Interface definition and implementation of interfaces

  • extendsAppear certain keywords represents a succession, or an extension, that is, sub-interface can get members of the parent interface. Note: Interface interfaces can be inherited, but can not inherit the class oh.
interface Countable {
    int NUM = 50;
}

interface Edible {
    String name = "Edible";
    void howToEat();
}
interface CreatureDoing extends Edible, Countable {
    public abstract void howToSleep();
}
  • Then let CreatureDoing interface inheritance Edible and Countable two interfaces, separated by commas, can be verified, the sub-interface access to members of the parent interface.
System.out.println(CreatureDoing.name);//Edible
System.out.println(CreatureDoing.NUM);//50
  • Interface can not be used to create an instance, the class implements the interface needs to be , then you need to use implementskeywords. Format is as follows:
//类实现多个接口
class 类 implements 接口1,接口2{}
//类继承某类,且实现多个接口
class 类1 extends 类2 implements 接口1,接口2{}
  • Implement similar interfaces and inheritance, interfaces member variables and methods can be obtained, but note that the implementation class all the abstract methods of the interface implemented need to be clearly given , otherwise the implementation class needs to be defined as an abstract class. This sentence, bit around, we know that the abstract methods in the interface implementation class is the need to achieve, we have added an interface can inherit an interface, a class can implement multiple interfaces. Then, if the interface has an abstract method A the method1, port B interface inheritance A, there is an abstract square method2, C class inherits both interfaces, C requires two abstract methods in a class, which is called to achieve All the abstract methods.

  • Another point is that implementation is actually an abstract interface methods to rewrite it, it is necessary to declare implementation is public , otherwise it does not meet the requirements of rewriting.

Interfaces and polymorphism

  • Interface can be as a kind of reference data types can be used to call its members direct interface, as above Creature.NUM.
  • Moreover, the interface can declare a reference type variable to point to an object of its implementation class , which is similar to let the parent class variable to point to a subclass of objects, polymorphism is well reflected.
//接口也实现了多态
Edible[] ediblesArray = new Edible[]{new Chicken(), new Orange()};
for (Edible edible : ediblesArray) {
    edible.howToEat();
}

Whether the interface is inherited from Object

  • See in the book saying: all interface types of reference variables can be directly assigned to a variable of type Object , so I was wondering, since this is not meant interface type is inherited from the Object class it?
    So I had to try, I call the method of the Object class with the interface types of variables:
System.out.println(cd.hashCode());

lYwpes.png

  • Interfaces and classes are always parallel relationship, they represent a reference data types, all classes inherit from Obejct class, but the interface is not. The reason why the interface type variable Object class methods can be invoked, because the interface implies a set of Object class and method signature exactly the same way , if not this way, then, is not able to compile successfully.
  • And the interface reference type variable can be assigned directly to the variables of type Object, also can be understood: the interface are ultimately required to achieve the implementation class, reference variables point to an instance of the implementation class, and classes are subclasses of Object achieve, so It is entirely reasonable.
package com.my.pac19;

/**
 * @auther Summerday
 */
public class TestEdible {
    public static void main(String[] args) {

        System.out.println(Countable.NUM);
        System.out.println(Edible.name);
        //接口 不能用于创建实例,但是可以声明引用类型变量指向其实现类的对象
        CreatureDoing cd = new Chicken();
        System.out.println(cd.hashCode());
        System.out.println();
        //接口也实现了多态
        Edible[] ediblesArray = new Edible[]{new Chicken(), new Orange()};
        for (Edible edible : ediblesArray) {
            edible.howToEat();
        }
    }
}

interface Countable {
    int NUM = 50;
}

interface Edible {
    String name = "Edible";

    public abstract void howToEat();
    //redundant 多余的

}

//接口继承接口
interface CreatureDoing extends Edible, Countable {
    public abstract void howToSleep();

}

abstract class Animal {

    public abstract void call();
}
class Chicken extends Animal implements Edible, CreatureDoing {
    //实现Edible中的howToEat方法
    @Override
    public void howToEat() {
        System.out.println("鸡要烤着吃");
    }
    //实现Animal中的call方法
    @Override
    public void call() {
        System.out.println("咯咯哒");
    }
    //实现Creature中的howToSleep方法
    @Override
    public void howToSleep() {
        System.out.println("呼呼");
    }
}
class Orange implements Edible {

    @Override
    public void howToEat() {
        System.out.println("橘子要剥皮吃");
    }
}

This article describes if inappropriate, welcome message exchange comments section!

Reference links:
https://stackoverflow.com/questions/6056124/do-interfaces-inherit-from-object-class-in-java?r=SearchResults
https://docs.oracle.com/javase/specs/jls/ se7 / html / jls-9.html # jls-9.2

Guess you like

Origin www.cnblogs.com/summerday152/p/12132349.html