为什么接口和抽象方法无法实例化? [英] why interface and abstract methods can't be instantiated?

查看:117
本文介绍了为什么接口和抽象方法无法实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么在java中限制接口和抽象类的实例化。我知道可以创建接口和抽象类的实现参考。我很清楚,但为什么它不能被实例化?任何人请帮助我

I am unable to figure out why instantiation of interface and abstract class is restricted in java. I know reference for implementation of interface and abstract class can be created. I am clear about that, but why it can't be instantiated? Anyone please help me

推荐答案

接口和抽象类的关键是提供一个必须在具体的类。

The point of both an interface and an abstract class is to provide an API which has to be implemented in a concrete class.

例如,假设我声明了这个接口:

For example, suppose I declare this interface:

public interface Foo
{
    int bar();
}

想象一下,如果有效代码:

And imagine if this were valid code:

Foo foo = new Foo();
int x = foo.bar();

x 的价值是多少?我们没有在任何地方指定 bar 的实现。这是一个毫无意义的调用,没有真正的实现来支持它。

What could the value of x be? We haven't specified an implementation of bar anywhere. It's a meaningless call, without a real implementation to back it up.

这篇关于为什么接口和抽象方法无法实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆