Chapter 9 - Interface

Think in java study notes

pikzas

2019.07.31

Chapter IX Interface

Knowledge Point

Abstract classes and methods 1. Abstract

1.1. An abstract class is designed to

The main function of the abstract class in order to achieve multi-state process, avoid errors created parent instance of the object and provides syntax support.
In standard practice, polymorphic, parent only provides a template for the subclasses, subclasses provide specific implementation. Under ordinary circumstances the parent class is a class, create an object of a parent class is meaningless. He did not specify the method of implementation.

1.2 What is an abstract method

abstract void f (); while not only declare methods thereof.

1.3 What is an abstract class

There are modified before class abstract class abstract class

1.4 Features abstract class

  • Can not be directly instantiated abstract class, subclass must be obtained by transformation of upwardly
  • If a class contains one or more abstract method, then the class as an abstract class must
  • If a class is an abstract class, an abstract method which may not (can have any number of conventional methods)
  • Abstract method modifier is not private, that prohibition is designed override, and abstract classes are contrary to
  • Abstract class can also define attributes and usage attributes of the same general category.
  • Subclass inherits the abstract class, if you implement all the abstract methods of the parent class, so he can declare a general category (also still be declared as an abstract class, the new abstract method can even be added), it can also be declared as abstract class, but if there are abstract methods are not fully implemented, the class must also be abstract class.

2. Interface

The interface is an extreme form of abstract method, all the methods are abstract claim

2.1. Designed interface

Apart order to more easily achieve polymorphism, a single inheritance more JAVA break limit, to achieve an implicit multiple inheritance (internal)

2.2. What is the interface

With the interface keyword instead of the class keyword indicated categories, which can have properties, can have only abstract methods (method name provided to determine the list of parameters and return values), but not to any implementation.

2.3 interface features

  • Based on the interface modifier modifiers public and may be default, and the effect is the same general category.
  • The default interface is public static final attribute, so it can be directly used by the interface point attributes, you can not make changes to the property. And you can not add access modifier private protected or default, will prompt compilation errors.
  • The default interface method is public abstract, and do not write public abstract modifier is also the default, you can not add these private protected default modifier.

Multiple inheritance in 3.JAVA

JAVA allows a class inherits from a single entity or a class is an abstract class, but can be more implement multiple interfaces.

public class Actor {
    public void fight(){
        System.out.println("do nothing");
    };
}

public interface CanFight {
    void fight();
}

public interface CanFly {
    void fly();
}

public interface CanSwim {
    void swim();
}

public class ActActor extends Actor implements CanFight,CanFly,CanSwim {
    @Override
    public void fly() {

    }
    @Override
    public void swim() {

    }
}

public class Test {
    public static void a(Actor actor) {
        actor.fight();
    }

    public static void b(CanFight canFight) {
        canFight.fight();
    }

    public static void c(CanFly canFly) {
        canFly.fly();
    }

    public static void d(CanSwim canSwim ) {
        canSwim.swim();
    }

    public static void main(String[] args) {
        ActActor actActor = new ActActor();
        a(actActor);
        b(actActor);
        c(actActor);
        d(actActor);
    }
}

In the above example Note fight () method implemented in ActActor did not do, this is because the Actor class has been achieved
if the fight will be the Actor class () method was changed to a non-public, then ActActor not equivalent to CanFight the fight () method to do to achieve the compiling error.
If the Actor class does not fight () method, but the Actor class inherits from Human, Human class has fight () method, then the time is also possible.

Here example also illustrates the usefulness of two interfaces

  • Subclasses can upcast to different base classes, code more flexible
  • Interfaces can not be instantiated, the compiler can prevent misuse.

4. The relationship between the interface and the interface, the interface with the class

Class can implement an interface, the interface can be inherited and can interface to multiple-inheritance interfaces


public interface Monster {
    void menace();
}

/**
 * 接口之间用继承
 */
public interface DangerousMonster extends Monster {
    void destory();
}

public interface Lethal {
    void kill();
}

/**
 * 接口可以多继承接口,只要用逗号隔开就行。
 */
public interface Vampire extends DangerousMonster,Lethal {
    void drinkBlood();
}


public class VeryBadVampire implements Vampire {
    @Override
    public void drinkBlood() {

    }

    @Override
    public void destory() {

    }

    @Override
    public void kill() {

    }

    @Override
    public void menace() {

    }
}

5. The combination of interfaces that can occur when the name conflict

Recall that if the parent of a class and its implementation of the interface has the same method when the class can no longer do the methods.
However, if more than one interface, which has the same method, resulting in overloading and cover simultaneously, this time may be an error will occur.


public interface InterfaceA {
    void f();
}

public class Father {
    public int f(){}
}

/**
* 如下写法的子类会提示编译错误,因为方法必须override,但是又会与父类的方法冲突(同名,同参数列表,就返回值不同,不是overload,所以会提示编译错误)
*/
public class Son extends Father implements InterfaceA {

}

6. The interface attributes public static final default (no include, in particular, is the default)

If the interface properties are the basic data types, they have capital, are compiler constants, can be used to enumerate the use of alternative
domain interfaces defined in the final is not empty, but may be constant expression initialize
these properties not part of the interface, the static storage area to store their value in interface

import java.util.Random;

public interface RandVals {
    Random RAND = new Random(47);
    int RANDOM_INT = RAND.nextInt(10);
    long RANDOM_LONG = RAND.nextLong() * 10;
}

7. nested interface

Class or interface may be nested within other interfaces

  • Interface nested within a class, in addition to the public, package access, private access to the more than
public class OuterClass {
    //1.类内部接口可以是包访问权限
    interface B{
        void f();
    }

    //1.1. 对应的实现类可以是public的
    public class BImpl implements B{
        public void f(){}
    }
    //1.2. 对应的实现类可以是private的
    private class BImpl2 implements B{
        public void f(){}
    }
    //1.3. 对应的实现类可以是默认访问权限的
    class BImpl3 implements B{
        public void f() {

        }
    }

    //2. 类内部接口可以是包访问权限
    public interface C{
        void f();
    }

    //2.1. 对应的实现类可以是public的
    public class CImpl implements C{
        public void f(){}
    }
    //2.2. 对应的实现类可以是private的
    private class CImpl2 implements C{
        public void f(){}
    }
    //2.3. 对应的实现类可以是默认访问权限的
    class CImpl3 implements C{
        public void f() {}
    }

    //2. 类内部接口可以是private的
    private interface D{
        void f();
    }

    //2.1. 对应的实现类可以是public的
    public class DImpl implements D{
        public void f(){}
    }
    //2.2. 对应的实现类可以是private的
    private class DImpl2 implements D{
        public void f(){}
    }
    //2.3. 对应的实现类可以是默认访问权限的
    class DImpl3 implements D{
        public void f() {}
    }

}

Guess you like

Origin www.cnblogs.com/Pikzas/p/11306424.html