Java 为什么抽象方法只能在抽象类中声明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22161297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:59:06  来源:igfitidea点击:

Why can abstract methods only be declared in abstract classes?

javaabstract-classabstractabstract-methods

提问by Jona

I understand that in abstract classes methods be both abstract, or not. But why can I not create an abstract method in a "normal", non-abstract class?

我知道在抽象类中,方法既是抽象的,也不是抽象的。但是为什么我不能在“正常”的非抽象类中创建抽象方法?

Thanks in advance for any explanation!

在此先感谢您的任何解释!

采纳答案by Warlord

Abstractmethod basically says, that there is no implementation of the method and it needs to be implemented in a subclass. However if you had an abstractmethod in a non-abstractclass, you could instantiate the class and get an object, that would have an unimplemented method, which you would be unable to call.

抽象方法基本上是说,没有方法的实现,它需要在子类中实现。但是,如果您在非抽象类中有抽象方法,则可以实例化该类并获取一个对象,该对象将具有未实现的方法,您将无法调用该方法。

回答by dasblinkenlight

Having an abstract method prevents a class from being instantiated, thus making it a de-facto abstract class. Java insists on you declaring this fact explicitly for consistency: technically, Java compiler does not need this additional mark in order to decide if a class is abstract based on the presence of abstract methods, but since you may want to make a class abstract without making any of its methods abstract, requiring the declaration on the class was the way to go.

拥有抽象方法可以防止类被实例化,从而使其成为事实上的抽象类。Java 坚持要求您显式声明这一事实以保持一致性:从技术上讲,Java 编译器不需要这个额外的标记来根据抽象方法的存在来确定一个类是否是抽象的,但是因为您可能希望使一个类抽象而不用它的任何方法都是抽象的,需要在类上声明是要走的路。

回答by user207421

Because having an abstract method makes it an abstract class. The reasoning is circular.

因为有一个抽象方法使它成为一个抽象类。推理是循环的。

回答by Mik378

You do wantto call a method providing an implementation, that's the essence of programming.

确实想调用提供实现的方法,这就是编程本质

Based on this idea, Java rules are:

基于这个想法,Java规则是:

  • You cannotinstantiate an abstract class.
  • You can onlyinstantiate a non-abstract class.
  • 不能实例化抽象类。
  • 只能实例化一个非抽象类。

What if Java let you call a method on an instance of a non-abstract class with...no implementation since method would be abstract=> no sense at all.

如果 Java 允许您在非抽象类的实例上调用方法,并且...没有实现,因为方法将是abstract=>根本没有意义

That's why Java and any other languages dealing with similar mechanism like C# (virtual method) prevent to declare an abstract method in a non-abstract class.

这就是 Java 和处理类似 C#(虚拟方法)等类似机制的任何其他语言阻止在非抽象类中声明抽象方法的原因。

回答by Siddharth

Lets start by understanding whywe need something like a abstractmethod. The answer is simple. I dont want my extendersto use my methods as it is, I want them to define their own behavior of a particular method. Since I use this method in other methods of my abstract class. I can provide a /**java doc**/ on the abstract class and point them to use a default behavior.

让我们首先了解为什么我们需要抽象方法之类的东西。答案很简单。我不希望我的扩展程序按原样使用我的方法,我希望他们定义自己的特定方法的行为。因为我在抽象类的其他方法中使用了这个方法。我可以在抽象类上提供一个 / ** java doc**/ 并指向它们使用默认行为。

class abstract LoveTheWorld {
    private int myKindness ;
    public int defaultGiveKindness() {
        myKindness -= 5 ;
        return 5 ;
    }
    /**
    you can use the defaultGiveKindness method, and not define your own behavior
    **/
    public abstract int giveKindness() ;
}

This also tells the extenderthat they can extendonly one class (as per java inheritance rules). Now, if you want to twist this story around, you can use interface instead of a abstract class. But it all depends on what constraints do you want your future developer to adhere to, strict or flexible. Strictwill keep it tight and ensure reduced mistakes, flexiblewill keep it loose and free and promote innovation. The question is **what do you need*8.

这也告诉扩展器他们只能扩展一个类(根据 java 继承规则)。现在,如果你想扭转这个故事,你可以使用接口而不是抽象类。但这一切都取决于您希望未来的开发人员遵守哪些限制,严格的还是灵活的严格将保持严密并确保减少错误,灵活将保持宽松和自由并促进创新。问题是**你需要什么*8。

回答by Gundamaiah

abstract methods are meant for leaving the implementation to the child classes.If the normal class contains abstract method,then one can creating the object for that class and may call the abstract method just like normal method.Then the problem will occur.That's is the reason abstract methods should be in abstract class (so that one cannot create the object for the abstract class)or interfaces only.

抽象方法是为了把实现留给子类。如果普通类包含抽象方法,那么可以为该类创建对象,并可以像普通方法一样调用抽象方法。那么就会出现问题。这就是原因抽象方法应该在抽象类中(这样就不能为抽象类创建对象)或只在接口中。

回答by helderdarocha

If a concrete class could have an abstract method, you would not be able to create an object from it. Imagine a Shapewith a concrete method getColor()and an abstract method draw(). It's great to referto some things in an abstract manner, so someone can tell you "render a shape!":

如果一个具体的类可以有一个抽象方法,你就不能从它创建一个对象。想象一个Shape具有具体方法getColor()和抽象方法的方法draw()。以抽象的方式引用某些东西是很好的,所以有人可以告诉你“渲染一个形状!”:

public void render(Shape s) { ... }

But when you use that reference to call draw(), you expect that the object referred by sknows how to do that:

但是当您使用该引用来调用 时draw(),您希望所引用的对象s知道如何做到这一点:

s.draw();

If concrete classes were allowed to have abstract methods, you could instantiate a Shape object, but when you called the draw method, it wouldn't know what to draw! Even if it knew how to say its color, position or 1000 other things. If it's not 100% specified, it can't exist as a working objects.

如果允许具体类具有抽象方法,您可以实例化一个 Shape 对象,但是当您调用 draw 方法时,它不知道要绘制什么!即使它知道如何说出它的颜色、位置或其他 1000 件事。如果不是 100% 指定,则它不能作为工作对象存在。

So Java requires that such classes be marked as abstract. Then you won't be able to use them to create objects, since they don't know how to concretely do 100% of the things that you expect from the object. You can only use abstract classes to refer to them. You can be sure now that only classes that have all their methods implemented will be used to create objects, and they will probably have names that seem less abstract too:

因此 Java 要求将此类类标记为抽象类。那么您将无法使用它们来创建对象,因为它们不知道如何具体地 100% 地执行您对对象的期望。您只能使用抽象类来引用它们。您现在可以确定,只有实现了所有方法的类才会用于创建对象,并且它们的名称也可能看起来不那么抽象:

Shape shape = new Rectangle();
render(shape);

Now you can say "render the shape" and you program, using reference to the rectangle, will know how to draw()it.

现在您可以说“渲染形状”,并且您使用对矩形的引用进行编程,将知道如何进行draw()

回答by Praveen Ramanayake

The simple answer is if the class is not abstract(concrete class) you could be able to instantiate that class and call any method of that class .but let's say if you had declared an abstract method in that non-abstract class-it's impossible to call that particular abstract method. (to prevent that, we can't declare abstract methods in non-abstract class )

简单的答案是,如果该类不是抽象类(具体类),则您可以实例化该类并调用该类的任何方法。但是假设您已在该非抽象类中声明了一个抽象方法 - 这是不可能的调用那个特定的抽象方法。(为了防止这种情况,我们不能在非抽象类中声明抽象方法)