Important Points about Interface in Java

Java中的接口提供绝对抽象,不能有构造器或方法体。接口的方法默认为public和abstract,可实现多继承。类通过implements关键字实现接口,需提供接口中所有方法的实现,除非是抽象类。推荐以接口而非实现来编写程序,以便于未来更换更好的实现。
摘要由CSDN通过智能技术生成

Important Points about Interface in Java

  1. interface is the code that is used to create an interface in java.

  1. Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with body.

  2. We can’t instantiate an interface in java.


  1. Interface provides absolute abstraction, abstract classes in java to provide abstraction but abstract classes can have method implementations but interface can’t.

  2. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.


  1. By default any attribute of interface is public, static and final, so we don’t need to provide access modifiers to the attributes but if we do, compiler doesn’t complain about it either.

  2. By default interface methods are implicitly public and abstract, it makes total sense because the method don’t have body and so that subclasses can provide the method implementation.


  1. An interface can’t extend any class but it can extend another interface. public interface Shape extends Cloneable{} is an example of an interface extending another interface. Actually java provides multiple inheritance in interfaces, what is means is that an interface can extend multiple interfaces.

  1. implements keyword is used by classes to implement an interface.

  2. A class implementing an interface must provide implementation for all of its method unless it’s an abstract class.


  1. We should always try to write programs in terms of interfaces rather than implementations so that we know beforehand that implementation classes will always provide the implementation and in future if any better implementation arrives, we can switch to that easily.

参考

Interface in Java.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值