33

I am now studying a java and I'm at the part of Abstract. I read sorta strange part to me that there is an abstract class which does not include any abstarct method.

Why do they use this kind of class?

Jacob
  • 41,721
  • 6
  • 79
  • 81
hongtaesuk
  • 557
  • 5
  • 10
  • 19
  • 2
    In JDK 1.00 an abstract method was required in abstract classes. 1.1 relaxed that restriction, and added classes such as `java.awt.event.KeyAdapter`. http://download.java.net/jdk7/docs/api/java/awt/event/KeyAdapter.html – Tom Hawtin - tackline Jul 28 '11 at 09:18

11 Answers11

42

To prevent instantiation of that class and use it only as a base class. Child classes can use the general methods defined in the abstract class.

For example it doesn't make sense to create an instance of AbstractVehicle. But All vehicles can reuse a common registerMileage(int) method.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 14
    +1 I think a "canonical" example would be `javax.servlet.HttpServlet`, which is `abstract` but has no abstract methods. The designers of that class simply don't want to allow you to register an `HttpServlet` as such in web.xml, as it's simply useless. – Costi Ciudatu Jul 28 '11 at 08:46
  • How about if I create an anonymous inner class out of the abstract class? In this way I can instantiate the abstract class without changing anything in it. – Abhishek Jain Jul 16 '17 at 18:30
8

A common reason to do this is to have the abstract class provide exploding implementations of the abstract methods as a convenience to subclasses who don't have to implement all the abstract methods, just those they want to - the remaining ones will still explode but it won't matter if those execution paths aren't exercised.

HttpServlet is an example of this pattern in action. It has default implementations for all methods that handle the different request types, but they all throw an exception. The subclass must override these if they want to do something meaningful. It's OK to leave some handler methods not overridden as long as they are never called.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

Yes, we can have abstract class without any abstract method. Best example of abstract class without any abstract method is HttpServlet

Maulik Kakadiya
  • 1,467
  • 1
  • 21
  • 31
2

If this class extends another abstract class and don't have implementation of inherited abstract methods.

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    In this scenario, it means that class **does have** abstract methods, inherited or not, so it's actually mandatory to declare it as `abstract`. The question is about the scenario when the compiler would not complain if you take the `abstract` keyword out of the class declaration. – Costi Ciudatu Jul 28 '11 at 08:40
  • @Costi Ciudatu yes. but not declared. – jmj Jul 28 '11 at 08:40
2

This class contains some common logic for all its inheritors, but itself does not represent usable entity (in terms of particular application)

1

Another example for having such class is when you implement creation helpers. These classes are used to ease the client in the creation of objects, which are related in topic but decoupled depending on the need. By nature, the methods of this creator classes are all static and they can be seen as utility classes as well.Obviously, instatntation of this classes is futile and hence the abstractkeyword.

To mention a recent example I met was the Sftpclass from org.springframework.integration.dsl.sftp which is basically an easy way to require objects (e.g: adapters, gateways) from the sftp api.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
1

These type of classes are used for a implement a general logic which can be implemented by other classes. Making it abstract prevents from instantiating it. But other classes can inherit the class and its methods.

Sumit
  • 736
  • 8
  • 26
1

Say you have a set of related classes, but no related (shared) code, yet. If we make all of these classes extend a base class with no abstract methods, that then if we wan't all of these classes to have an identical method/feature in the future, that can be done in one shot by putting it in the base class. So code is not repeated and it reflects in all child classes by including it in just one place.

Kumar Manish
  • 1,166
  • 1
  • 16
  • 28
0

I develop a abstract class to prevent instantiation of that class and use it only as a base class. because, These type of classes are used for a implement a general logic which can be implemented by other classes. Sometimes, I have a default implementation for every method in abstract class. In the manner, it doesn't force the sub-class to override all of method, but also it implement everyone that is need.It means implicitly you have to override at least one method to make scene using this abstract class.

user3359139
  • 430
  • 4
  • 17
-1

I can't think of any good reason to use it. It could be used as "marker" but an interface would be a better choice.

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
-4

Abstract class without abstract method means you can create object of that abstract class. See my Example.

abstract class Example{
void display(){
 System.out.println("Hi I am Abstract Class.");
}
}
class ExampleDemo 
{
public static void main(String[] args) 
{
Example ob = new Example(){};
ob.display();
}
}

If you write one abstract method inside abstract class then it will not compile. Which means if you create abstract class without abstract method then you can create Object of that Abstract Class.

lal
  • 5
  • 1
  • Your example does not create an instance of the abstract `Example` class. It creates an instance of a non-abstract, anonymous inner class that `extends Example`. Compile your program and look at the .class files that the compiler generates. You'll see what I mean. – Solomon Slow Jan 28 '15 at 15:14
  • Besides, that would be an unstable side-effect at best. To say that it means that "...you can create [an] object of that abstract class" would be a dangerous proposition because what would happen to the implementation if you later added an abstract method to your abstract class? – Draghon May 30 '15 at 17:07
  • you can't create an instance of an abstract class, your example is mistaken – jucardi Oct 05 '16 at 21:57