Question

I'm confused because I found two concepts in my book which I think are incorrect. Help me please to clarify these two points.

  1. 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.

  2. 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?

Was it helpful?

Solution

  1. Wrong. You cannot declare abstract methods in non abstract classes.

  2. 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();
}

OTHER TIPS

The book is totally wrong. I would recommend recycling it.

  1. A class containing abstract methods must be declared abstract
  2. An abstract method does not need to be declared public - it can be anything except private
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top