java 接口可以多继承

接口是常量值和方法定义的集合。接口是一种特殊的抽象类。

java类是单继承的。classB Extends classA

java接口可以多继承。Interface3 Extends Interface0, Interface1, interface……

以下是spring ApplicationContext 接口的代码,同时继承了多个接口

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

	/**
	 * Return the unique id of this application context.
	 * @return the unique id of the context, or {@code null} if none
	 */
	String getId();

	/**
	 * Return a name for the deployed application that this context belongs to.
	 * @return a name for the deployed application, or the empty String by default
	 */
	String getApplicationName();

	/**
	 * Return a friendly name for this context.
	 * @return a display name for this context (never {@code null})
	 */
	String getDisplayName();

	/**
	 * Return the timestamp when this context was first loaded.
	 * @return the timestamp (ms) when this context was first loaded
	 */
	long getStartupDate();

	/**
	 * Return the parent context, or {@code null} if there is no parent
	 * and this is the root of the context hierarchy.
	 * @return the parent context, or {@code null} if there is no parent
	 */
	ApplicationContext getParent();

	/**
	 * Expose AutowireCapableBeanFactory functionality for this context.
	 * <p>This is not typically used by application code, except for the purpose of
	 * initializing bean instances that live outside of the application context,
	 * applying the Spring bean lifecycle (fully or partly) to them.
	 * <p>Alternatively, the internal BeanFactory exposed by the
	 * {@link ConfigurableApplicationContext} interface offers access to the
	 * {@link AutowireCapableBeanFactory} interface too. The present method mainly
	 * serves as a convenient, specific facility on the ApplicationContext interface.
	 * <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException
	 * after the application context has been closed.</b> In current Spring Framework
	 * versions, only refreshable application contexts behave that way; as of 4.2,
	 * all application context implementations will be required to comply.
	 * @return the AutowireCapableBeanFactory for this context
	 * @throws IllegalStateException if the context does not support the
	 * {@link AutowireCapableBeanFactory} interface, or does not hold an
	 * autowire-capable bean factory yet (e.g. if {@code refresh()} has
	 * never been called), or if the context has been closed already
	 * @see ConfigurableApplicationContext#refresh()
	 * @see ConfigurableApplicationContext#getBeanFactory()
	 */
	AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

}

不允许类多重继承的主要原因是,如果A同时继承B和C,而B和C同时有一个D方法,A如何决定该继承那一个呢?

但接口不存在这样的问题,接口全都是抽象方法继承谁都无所谓,所以接口可以继承多个接口。

注意:

1)一个类如果实现了一个接口,则要实现该接口的所有方法。

2)方法的名字、返回类型、参数必须与接口中完全一致。如果方法的返回类型不是void,则方法体必须至少有一条return语句。

3)因为接口的方法默认是public类型的,所以在实现的时候一定要用public来修饰(否则默认为protected类型,缩小了方法的使用范围)。

### Java接口多继承的技术细节 在Java中,接口多继承是被允许的。这意味着一个接口可以继承多个其他接口[^1]。通过这种方式,接口多继承可以让类具备多种行为,并且有助于解耦合代码,从而提升代码的可维护性和可扩展性。 接口多继承的具体实现方式可以通过`extends`关键字完成。例如,以下代码展示了如何让一个接口继承多个接口: ```java interface A { void methodA(); } interface B { void methodB(); } interface C extends A, B { // 接口C继承了接口A和B void methodC(); } ``` 在上述示例中,接口`C`继承了接口`A`和`B`,这表明任何实现接口`C`的类都必须实现`methodA()`、`methodB()`以及`methodC()`方法[^2]。 此外,一个类也可以同时实现多个接口。这种情况下,类需要提供所有接口中定义的方法的具体实现。例如: ```java class D implements A, B { @Override public void methodA() { System.out.println("Implementation of methodA"); } @Override public void methodB() { System.out.println("Implementation of methodB"); } } ``` 需要注意的是,尽管接口支持多继承,但Java中的类只能单继承。也就是说,一个类只能继承自一个直接父类,但可以通过实现多个接口来模拟多继承的行为[^4]。 关于接口中的成员特性,接口中的变量默认是`public static final`类型的,而接口中的方法默认是`public abstract`类型的。这些默认修饰符确保了接口成员的通用性和抽象性[^2]。 最后,当接口之间存在继承关系时,子接口会继承父接口的所有方法和常量。如果子接口中定义了与父接口同名的方法,则子接口的方法会覆盖父接口的方法定义[^3]。 ### 注意事项 - 接口多继承并不等同于类的多继承。类的多继承Java中是不被允许的,但可以通过实现多个接口来达到类似的效果。 - 在设计接口时,应尽量避免接口之间的循环依赖或重复定义,以保持代码的清晰和简洁。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值