2

I know we can define abstract methods inside abstract class only. I also know we can't instantiate abstract class on its own, it must be inherited (extended). And I know that abstract method forces subclasses to provide an implementation for that method.

My question is only if I can define that abstract method without the "abstract" keyword? Something like:

public abstract Animal
{ 
    public void makeNoise();
}
3
  • 1
    "I know we can define abstract methods inside abstract class only." that is not true. All non-default methods of interfaces are public and abstract without using abstract keyword.
    – Pshemo
    Jan 17, 2018 at 19:59
  • 1
    Is your Animal class or interface? Please write proper Java syntax for places that you are not asking question about.
    – tsolakp
    Jan 17, 2018 at 20:02
  • "And I know that abstract method forces subclasses to provide an implementation for that method" that is true only for not abstract subclass. Abstract subclasses can even add more abstract methods and still not implement inherited abstract ones.
    – Pshemo
    Jan 17, 2018 at 20:09

3 Answers 3

3

You can create an interface which is a set of abstract methods without using abstract keyword. If you create an abstract class, then every abstract method should have the abstract keyword.

Check the documentation for more info.

2

My question is only if I can define that abstract method without the "abstract" keyword?

You cannot define abstract methods that way.
An abstract class can contain the implementation of a method. Because that, you have to mark which method is abstract and which is not.

If you create Animal class

public abstract class Animal {
    public void makeNoise();
}

Eclipse will suggest you either add body or change the method to abstract.

1

My question is only if I can define that abstract method without the "abstract" keyword?

No you can't on classes. That's only possible on interfaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.