Interface interface

java interface is achieved through interface

Why have an interface?

java no multiple inheritance can interface to achieve multiple inheritance feature 
because classes are the same two things in common extracted, and the interface can be seen as two different things, one of the characteristics extracted. 

      EG: Xiaodong and his dogs will eat, do not they both the same kind of thing it ? certainly not in. so they will eat this feature can be extracted into an interface using 

the interface plainly that is, to complete the class unreasonable some of the features exist to make up classes short board

 

Interface features

* 
 * 1 : interfaces utilized
  *       interface to define
  * 
 * 2 : Java classes and interfaces are two parallel structures
  * 
 * 3 : The definition of the interface members
  *     JDK7 and previously: the interface can define the global constants and abstract methods
  *> global constants: public  static  Final : but the writing is often omitted public   static fianl
  *> abstract methods: public  abstract 
 * 
 *     jdk8: In addition to defining global constants and abstract methods, can also define a static method, the default method
  * 
 * 
 * 4 : not define the interface constructor, i.e. it can not be instantiated
  * 
 * 5: development in java class to implement by having a few mouthfuls ( the implements used) manner,
  *           If implemented covers all methods abstract class interface, this implementation can instantiate class
  *            If the class does not implement all the abstract methods finished weight, this implementation class is an abstract class
  * 
 * 
 * 
 *. 6: Java class implements multiple interfaces -----> make up for the limitations of single inheritance java
  * format: class   AA the implements BB, CC, DD, EE
  * 
 * 
 * 7 : the interface between the interface and can be inherited, but also multiple inheritance
  * 
 * 8 : specific uses its interface: polymorphic reflected
  * 
 * 9: in fact, the interface can be seen as a specification

 

 

 

Interview questions:

Troubleshooting

interface A { int x = 0;
}
class B {
    int x = 1;
}
class C extends B implements A {
    public void pX() {
System.out.println(x); }
    public static void main(String[] args) {
        new C().pX();
} }

 

x pX () method does not declare formal parameters, so there is a problem here

 

interface Playable {
    void play();
}
interface Bounceable {
    void play();
}
interface Rollable extends Playable, Bounceable {
     Ball ball = new Ball("PingPang");
}


class Ball implements Rollable {
    private String name;
    public String getName() {
        return name;
}
    public Ball(String name) {
        this.name = name;
}
public void play() {
ball = new Ball("Football"); System.out.println(ball.getName());
} }
Rollable interface method does not override inherited interface, 
Ball in ball properties are not override this variable is defined as defined Rollable interface, the 
interface is a variable time constant can not be changed

 

 

 

Exercises

Define an interface for implementing the comparison of two objects. Ø interface CompareObject { 
} 
l Circle define a class declaration redius attribute getter and setter methods provide 
l ComparableCircle define a class that inherits the interface CompareObject Circle class and implements. Implement peer interface method given in ComparableCircle compareTo class used to compare two of the radius of the circle. 
 define a test class InterfaceTest, create two ComparableCircle object, call the radius of the compareTo method to compare two classes. 
 Consideration: referring to the practice of the rectangle defined class Rectangle and ComparableRectangle classes, given in ComparableRectangle compareTo method implemented in the class, comparing two rectangular areas

 

Guess you like

Origin www.cnblogs.com/zhaoyunlong/p/11697180.html