接口真的继承了Java中的Object类吗?

本文探讨了Java中接口是否继承自Object类的问题。解释了接口不能直接继承类,但如何间接地实现Object方法的调用。通过内部机制,接口会声明与Object公共实例方法相对应的公共抽象成员方法,除非接口已明确声明相同签名的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:
这是一篇国外博客,个人觉得解释得还行。没有翻译的原因是本人翻译水平并不太佳,如果直接翻译可能会与原文意思有所出入,请大家自行翻译(如果英语水平不错),如果英语水平不太行可以利用百度翻译结合自己的理解去看

原文链接:http://geekexplains.blogspot.com/2008/06/do-interfaces-really-inherit-from-class.html

Do Interfaces really inherit the class Object in Java (the cosmic sperclass)?

Well… the answer is NO. An interface can’t inherit from a class in Java, not at least directly. So, we can safely say that interfaces don’t inherit from the Object class. Okay… so how can they do that indirectly? We know that interfaces can have classes declared as members as well just like they can have constants. Such a class is called a member class and like constants all the member classes of an interface would be static and public. And that static member class (like any other class in java) inherits Object class.

But, how are we able to compile code having Object method calls on the references of an interface type in Java? We all know that the object of the implementing class (and hence its type) will be assigned to only at run time and we can compile a code only if the compiler finds a method of that signature in the declared type (either declared directly or inherited from superclasses). This is absolutely correct for classes in Java, but only partially correct for interfaces in Java. Surprised? Let’s try to understand what internally happens in case of interfaces.

The Java Language Specification clearly says that the members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct superinterface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface. This is what makes the signatures of the Object methods available to the compiler and the code compiles without any error. Remember if the interface tries to declare a public instance method declared ‘final’ in the Object class then it’ll result into a compile-time error. For example, ‘public final Class getClass()’ is a public instance method declared ‘final’ in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail.

Is this Inheritance of Object methods by the Interfaces?

No. This can not be termed as ‘Object methods being inherited by the Interfaces’. This is just a special treatment given to the interfaces in Java.

In this case all the qualified (public instance) Object class methods are declared as public abstract, which is not inheritance. Right? In inheritance we get the definition of the method as well and the non-abstract methods are not inherited as ‘abstract’. But an interface in Java can’t have any of these two - definition or non-abstract method. Hence, the designers of Java had to think of an alternative.

Moreover, only public instance methods are implicitly declared in the interfaces and what about other methods - for example, protected Object clone() and protected void finalize()? In case of inheritance they are also inherited by the subclasses.

Thus, we see that it’s not exactly inheritance of the Object class by the interfaces. An interface can’t inherit a class for the simple reason that interfaces can only have abstract methods (i.e., they can’t have body). Please don’t say that we can have an abstract class having only abstract methods which can be inherited safely by interfaces 😃 We will better have an interface in that case.

Example: a simple Java program showing Object method access on interface ref type

package test;

public class TestInterfaceDemo{



	public static void main(String[] args) {



	TestInterface testInterface = new TestInterfaceImpl();
	
	//... calling Object class method - toString - OK
	
	System.out.println(testInterface.toString());
	
	//... calling the interface method - testMethod - OK
	
	testInterface.testMethod();
	
	//... calling the implementing class method - implClassMethod - ERROR
	
	//testInterface.implClassMethod();
	
	//... calling the same method after casting the reference - OK
	
	((TestInterfaceImpl)testInterface).implClassMethod();
	
	
	
	}



}


package test;

public class TestInterfaceImpl implements TestInterface{

	public void testMethod(){
	
		System.out.println("Test Method if the Interface");
	
	}

	public void implClassMethod(){
	
		System.out.println("Test Interface Impl Class Method");
	
	}

}


Output:-
test.TestInterfaceImpl@45a877 (variable)
Test Method if the Interface
Test Interface Impl Class Method

If we uncomment the line ‘//testInterface.implClassMethod();’ for accessing a method of the implementing class which is not a member of the interface type then expectedly we get a compiler error:-

Error(14,23): method implClassMethod() not found in interface test.TestInterface

As at the compiler doesn’t know the type of the assigned object and hence can’t resolve the signature of the method call on the declared reference type during compilation and therefore report an error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值