Вопрос

I understand an abstract class may contain abstract and concrete methods (i.e with body implementation). My question are: can subclasses inherit/override concrete methods from an abstract superclass. And secondly do they have to implement concrete methods the same way they implement abstract methods?

Это было полезно?

Решение

can subclasses inherit/override concrete methods from an abstract superclass

If they are not final, yes, they can be overriden.

And secondly do they have to implement concrete methods the same way they implement abstract methods?

No, they only need to implement/override the abstract ones, otherwise an abstract method wouldn't make sense.

Другие советы

A concrete method means, the method has complete definition but it can be overridden in the inherited class. If we make this method "final" then it can not be overriden. Declaring a method or class "final" means its implementation is complete. It is compulsory to override the abstract methods in the subclass otherwise the subclass would also be an abstract class and has to be declared abstract.

Can subclasses inherit/override concrete methods from an abstract superclass ?

Subclasses will inherit all the methods which are marked public or protected, if the subclass is in a different package than the parent class. If the subclass is in the same package, it inherits all the methods except private methods.

The subclass has to override/implement abstract methods and can override/implement concrete methods if they are not marked as final.

Do they have to inherit concrete methods the same way they inherit abstract methods?

No. They don't have to implement the concrete methods. But, they can override the concrete methods, unless they are not marked final.

Concrete methods in Java are nothing but just like any other normal methods. The methods which are not abstract methods are called to be concrete methods in java. If we want to execute those concrete methods create an instance(object) of the class and call to that specific method.

If you declare an abstract method in a class then you must declare the class abstract as well. you can’t have an abstract method in a concrete class. In Java, it is not possible to instantiate an abstract class. An abstract class may contain abstract and concrete methods (i.e with body implementation).

Yes, subclasses inherit/override concrete methods from an abstract superclass if they are not private, final or static, they can be overridden.

No, They don't have to implement the concrete methods. But, they can override the concrete methods, unless they are not marked final.

Can subclasses inherit concrete methods from an abstract superclass.

They >>do<< inherit them, unless they override them. (Apart from private methods, which are never inherited or overridden in Java.)

Can subclasses override concrete methods from an abstract superclass.

Yes they can override them. (Apart from private methods, which are never inherited or overridden in Java.) They don't have to though.

And secondly do they have to implement concrete methods the same way they implement abstract methods?

If you are asking if a subclass has to "implement" a concrete method that is defined in an abstract superclass:

No they do not have to, but they can, and if they do that is called overriding.

If you are asking if overriding a concrete method is the same as implementing an abstract method:

No, not completely the same. In an implementation of a abstract method, you typically cannot use super to delegate the overridden method defined further up the superclass chain.

If you are asking if an override of a concrete method looks the same as an implementation of an abstract method:

Yes. The syntax is the same. (You just can't delegate using super in the latter case; see above.)


Note: pretty much all of the above is also true when the subclass is also abstract; i.e. an abstract subclass of an abstract superclass.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top