Reputation: 2091
I'm confused because I found two concepts in my book which I think are incorrect. Help me please to clarify these two points.
As soon as a class has one or more abstract methods, this class is abstract, even if it is not declared abstract (although it is highly recommended to do so). This is correct:
class A
{
public abstract void f() ; // OK
.....
}
Nevertheless, A is considered abstract and an expression such as new A(...) will be rejected.
An abstract method must be declared public, which is logical since its purpose is to be redefined in a subclass.
The first point give me an error and the second is not necessary. Am I on the right path?
Upvotes: 1
Views: 73
Reputation: 425418
The book is totally wrong. I would recommend recycling it.
abstract
methods must be declared abstract
public
- it can be anything except private
Upvotes: 3
Reputation: 121840
Wrong. You cannot declare abstract
methods in non abstract
classes.
Wrong. You can have protected
or package-local abstract
methods as well.
There is one special rule about interfaces: methods in interfaces are always public abstract
. It is redundant to specify any of these modifiers when defining an interface:
public interface Foo
{
/* public abstract is implied here */ void bar();
}
Upvotes: 3