Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java can an abstract method be anything other than public?

Tags:

java

In java can an abstract method be anything other than public? Are abstract methods implicitly public or are they package if you don't specify? (regular methods are implicitly package right?) are there any visibility modifiers that an abstract method can't have? (private strikes me as problematic)

like image 690
David Avatar asked May 15 '11 03:05

David


People also ask

Can abstract method be private in Java?

If a method of a class is private, you cannot access it outside the current class, not even from the child classes of it. But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use. Therefore, the abstract method cannot be private.

Can a abstract have private methods?

Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).

Can there be an abstract method without an abstract class?

Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.

Are abstract methods public by default in Java?

By default, all methods are public and abstract until we do not declare it as default and properties are static and final.


1 Answers

abstract methods have the same visibility rules as normal methods, except that they cannot be private.

like image 134
SLaks Avatar answered Sep 18 '22 17:09

SLaks