简体   繁体   中英

abstract method

What is the difference between abstract method and method overriding in java? Because same result can be found by using method overriding. so what is the necessity of abstract method.

An abstract method forces a programmer who inherits the method to define an implementation for it, ie the class says "I will need an implementation of this function, which the concrete implementation of this class must provide".

This is in contrast to overriden methods, which allow (rather than require ) the inheriting class to change the implementation of the method for objects of that class.

In particular, overridable methods (or "virtual" methods) have a basic implementation (which can itself be empty) that the overriding method can call.

The abstract method needs to be implemented in order for the subclass to be utilized. You must also implement non-abstract methods, for instance, in the case of interface method signatures.

I think it is correct to say that you implement abstract methods in the same way that you implement method stubs of an interface. Overriding is not the same as implementing because you can override methods which are not stubs. Subclasses often override methods where the superclass is neither an interface nor an abstract class. So the term overriding has a broader meaning than implementation.

You may find this tutorial helpful as it has quite a clear definition.

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Hope that helps!

The two have different usages which can't be compared.

An Abstract Class is created only to be sub classed by some other class. The implementing class necessarily has to implement all the abstract methods declared inside it. However Method Overriding is the ability of a subclass to override a method from an inherited superclass whose behavior is "close enough" and then to modify behavior as needed. An Abstract Method is one that is declared without an implementation and needs to be necessarily implemented in the subclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM