java interface 接口

  1. 接口

An interface can have methods and variables ,but the methods decleared in interface are by default abstract(only method signature ,no body).

Interfaces specify what a class must do and not how. It is the blueprint of the class.

Interface就相当于干爹,可以被继承,而且专门为了继承。

为什要用interface?

  1. it is used to achieve total abstraction,
  2. since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance.

有抽象类,为什么要用interface?

1.)abstract class may contain non final variables, whereas variables in interface are final, public and static.

To implement an interface we use keyword:implement

 

扫描二维码关注公众号,回复: 8773172 查看本文章

 

Summary

  1. we cannot create instance of interface but we can make a reference of it that can refer to the object of its implementing class.
  2. A class can inplement more than one interface.
  3. A  class that implements interface must implements all the methods in interface.
  4. All the methods are public and abstract. And all the fields are public, static and final.
  5. It is used to achieve multiple inheritance.
发布了83 篇原创文章 · 获赞 18 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/Abudula__/article/details/82902680
今日推荐