Defining an abstract class without any abstract methods

123,763

Solution 1

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.

Solution 2

Yes you can do it. Why don't you just try doing that?

Solution 3

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.

Solution 4

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.

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

Share:
123,763
VisaMasterCard
Author by

VisaMasterCard

Updated on April 21, 2020

Comments

  • VisaMasterCard
    VisaMasterCard about 4 years

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

  • Marc W
    Marc W over 13 years
    Nice idea for taking up the minimum 30 character limit.
  • Marc W
    Marc W over 13 years
    And it's not just the OP. Look at all the upvotes the question has gotten!
  • biziclop
    biziclop over 13 years
    @karim79 There's no need for high horses and all that sauce-pouring stuff. Trying it out doesn't quite reveal why it is allowed, while asking it might.
  • biziclop
    biziclop over 13 years
    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.
  • karim79
    karim79 over 13 years
    @bizclop - Trying it out for himself might have yielded a more productive question. There are no high horses. This is pretty straightforward. His question was 'Can I' rather than 'Why does this...'.
  • Tyler Treat
    Tyler Treat over 13 years
    If the question can be answered with a simple yes or no, then the asker should consider rephrasing, using Google, or simply trying it out.
  • Gordon Gustafson
    Gordon Gustafson over 13 years
    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.
  • biziclop
    biziclop over 13 years
    @karim79 Yeah, the question could've been phrased better but I guess it doesn't take that much effort to look one step ahead.
  • supercat
    supercat over 10 years
    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.
  • Usman Rana
    Usman Rana about 7 years
    If there is no abstract method then how can an interface be used? Interface has all the methods abstract by default.
  • karlihnos
    karlihnos about 5 years
    Why shouldnt I want that the class can not be instantiated? Any good reason, example?