简体   繁体   English

为什么接口和抽象方法无法实例化?

[英]why interface and abstract methods can't be instantiated?

I am unable to figure out why instantiation of interface and abstract class is restricted in java. 我无法弄清楚为什么在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. 接口和抽象类的要点是提供必须在具体类中实现的API。

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();

What could the value of x be? x的价值是什么? We haven't specified an implementation of bar anywhere. 我们没有在任何地方指定bar的实现。 It's a meaningless call, without a real implementation to back it up. 这是一个毫无意义的电话,没有真正的实施来支持它。

the If you think of a class as blueprints for creating (instantiating) an instance, much like the blueprints for a house tell you how to build a house. 如果你认为一个类是创建(实例化)实例的蓝图,就像房子的蓝图告诉你如何建造一个房子。 Think of an interface as a floorplan for the house - its an incomplete view (specification) of the house. 将界面视为房屋的平面图 - 它是房屋的不完整视图(规格)。 There isn't enough detail to build the house from it - its only an outline of the rooms. 没有足够的细节来建造房子 - 它只是房间的轮廓。 An abstract method is worse - its just the outline of one room. 抽象方法更糟糕 - 它只是一个房间的轮廓。

Interfaces and Abstract classes are not concrete classes. 接口和抽象类不是具体类。 They are deamed to be incomplete and not to be created. 他们被贬低为不完整而不被创造。 You can use a subclass or implementing class. 您可以使用子类或实现类。

An Abstract class is a class that is not fully implemented. Abstract类是未完全实现的类。 You want to force the developer to implement all the abstract parts of the class BEFORE he/she can instanciate it. 您希望强制开发人员在他/她可以实例化之前实现该类的所有抽象部分。

An Interface is a contract that a class must respect. 接口是类必须遵守的合同。 As such, it cannot be instanciated. 因此,它无法实现。 It can be important to define an Interface that a set of classes must respect in the case of a plugin system for example : All you plugins will share the same interface and thus will be inter-changeable. 例如,在插件系统的情况下定义一组类必须遵守的接口可能很重要:所有插件将共享相同的接口,因此可以互换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果抽象类无法实例化,为什么会出现以下情况? - If an abstract class can't be instantiated, why is the following possible? 为什么我们不能在不修改“ public”的情况下实现从接口到抽象类的方法? - why we can't implement methods from interface to abstract class, without modifying “public”? 为什么抽象类不能扩展接口? - Why can't an abstract class extend an interface? 为什么Java中的静态方法不能是抽象的? - Why can't static methods be abstract in Java? 为什么抽象类可以像这样实例化? - Why can abstract class be instantiated like this? 当我们在一个接口中添加两个抽象方法并只实现一个方法时,为什么我们不能使用 lambda 实现另一个方法? - When we add two abstract methods in an interface and implement just one method then why can't we implement the other method using lambda? 为什么尽管无法实例化接口,我们仍可以创建接口实例变量? - Why we can create an interface instance variable although Interface can't be instantiated? 为什么抽象对于接口隐式方法不起作用? - Why abstract does not work for interface implicit methods? 为什么实现接口的抽象类可能会错过接口方法之一的声明/实现? - Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods? 为什么我们不能在功能界面中重载抽象方法? (JAVA) - Why can't we overload a abstract method in a functional interface? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM