131

Can I define an abstract class without adding an abstract method?

2

11 Answers 11

193

Of course.

Declaring a class abstract only means that you don't allow it to be instantiated on its own.

Declaring a method abstract means that subclasses have to provide an implementation for that method.

The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around.

0
11

Yes you can. The abstract class used in java signifies that you can't create an object of the class. And an abstract method the subclasses have to provide an implementation for that method.

So you can easily define an abstract class without any abstract method.

As for Example :

public abstract class AbstractClass{

    public String nonAbstractMethodOne(String param1,String param2){
        String param = param1 + param2;
        return param;
    }

    public static void nonAbstractMethodTwo(String param){
        System.out.println("Value of param is "+param);
    }
}

This is fine.

0
7

YES You can create abstract class with out any abstract method the best example of abstract class without abstract method is HttpServlet
Abstract Method is a method which have no body, If you declared at least one method into the class, the class must be declared as an abstract its mandatory BUT if you declared the abstract class its not mandatory to declared the abstract method inside the class.

You cannot create objects of abstract class, which means that it cannot be instantiated.

0
5

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.

1
  • Why shouldnt I want that the class can not be instantiated? Any good reason, example?
    – karlihnos
    Commented Mar 15, 2019 at 15:57
4

Yes, you can declare a class you cannot instantiate by itself with only methods that already have implementations. This would be useful if you wanted to add abstract methods in the future, or if you did not want the class to be directly instantiated even though it has no abstract properties.

3
  • The question wasn't about empty abstract classes, just abstract classes without abstract methods. There's no valid reason as far as I can see for using a completely empty abstract class.
    – biziclop
    Commented Jan 27, 2011 at 0:23
  • 1
    Answer fixed. Using an empty abstract class could be useful if you had good reason to want to add abstract methods later and not have to deal with refactoring any other parent classes. Commented Jan 27, 2011 at 0:28
  • 1
    In the middle of a hierarchy, a completely empty (except for the inheritance spec) abstract class may make sense if derivations of that class will have different contractual obligations from those of other derivations of the parent.
    – supercat
    Commented Dec 29, 2013 at 16:41
3

yes, we can declare an abstract class without any abstract method. the purpose of declaring a class as abstract is not to instantiate the class.

so two cases

1) abstract class with abstract methods.

these type of classes, we must inherit a class from this abstract class and must override the abstract methods in our class, ex: GenricServlet class

2) abstract class without abstract methods.

these type of classes, we must inherit a class from this abstract class, ex: HttpServlet class purpose of doing is although you if you don't implement your logic in child class you can get the parent logic

please check the HttpServlet source code

3

You can, the question in my mind is more should you. Right from the beginning, I'll say that there is no hard and fast answer. Do the right thing for your current situation.

To me inheritance implies an 'is-a' relationship. Imagine a dog class, which can be extended by more specialized sub types (Alsatian, Poodle, etc). In this case making the dog class abstract may be the right thing to do since sub-types are dogs. Now let's imagine that dogs need a collar. In this case inheritance doesn't make sense: it's nonsense to have a 'is-a' relationship between dogs and collars. This is definitely a 'has-a' relationship, collar is a collaborating object. Making collar abstract just so that dogs can have one doesn't make sense.

I often find that abstract classes with no abstract methods are really expressing a 'has-a' relationship. In these cases I usually find that the code can be better factored without using inheritance. I also find that abstract classes with no abstract method are often a code smell and at the very least should lead to questions being raised in a code review.

Again, this is entirely subjective. There may well be situations when an abstract class with no abstract methods makes sense, it's entirely up to interpretation and justification. Make the best decision for whatever you're working on.

1
  • "In these cases I usually find that the code can be better factored without using inheritance." How specifically better factored?
    – Blaisem
    Commented Oct 20, 2022 at 17:46
2

Yes you can. Sometimes you may get asked this question that what is the purpose doing this? The answer is: sometimes we have to restrict the class from instantiating by its own. In that case, we want user to extend our Abstract class and instantiate child class

1

yes you can do that.

declaring class abstract means that class will not be instantiated by any other class.

and there should be at least one abstract method inside that and meaning of that you can declare abstract method in that class if you are not declaring method than its ok.

example:

public abstract class abs {

    protected int cx = 0, cy = 0;

    public void p() {
        System.out.print("hello");
    }
}

this will work for sure.

0
1

Actually there is no mean if an abstract class doesnt have any abstract method . An abstract class is like a father. This father have some properties and behaviors,when you as a child want to be a child of the father, father says the child(you)that must be this way, its our MOTO, and if you don`t want to do, you are not my child.

0

Yes, you can define an abstract class without an abstract method. However, if there is no method inside you might better go with an interface

2
  • 4
    Nice idea for taking up the minimum 30 character limit.
    – Marc W
    Commented Jan 27, 2011 at 0:16
  • 1
    If there is no abstract method then how can an interface be used? Interface has all the methods abstract by default.
    – Usman Rana
    Commented Mar 8, 2017 at 14:45

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