Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point in having an abstract class with no abstract methods?

Can have an abstract class implementing all of its methods-- with no abstract methods in it.

Eg.:

public abstract class someClass { 
    int a; 
    public someClass (int a) { this.a = a; } 
    public void m1 () { /* do something */ } 
    private void m2 () { /* do something else */ }  
}

What's the advantage, if any, of having such an abstract class compared to having the same class as a concrete one instead?

One i can think of is that, when i declare it as abstract, it won't be instantiated. however, i can have the same effect by making it concrete and its constructor(s) private.

TIA.

//==================

EDIT: One other use I can think of:

it may be extending another abstract class or implementing an interface without implementing that class's abstract methods-- although it is implementing all methods of its own. for whatever it' worth.

like image 979
user3880721 Avatar asked Sep 05 '14 19:09

user3880721


People also ask

Why we need abstract class without abstract method?

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.

Can I have abstract class without abstract methods?

An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

What is the point of having an abstract class?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

What happens if I will not provide an abstract method in abstract class and interface?

If you don't a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.


2 Answers

It has a conceptual meaning: this class has a behaviour which makes no sense on its own.

Granted, it's difficult to imagine such a scenario without well-defined extension points (i.e. abstract methods), but occasionally it will be a reasonably accurate model of your problem.

You can have something like this:

public abstract class ObjectWithId {
    private final String id;

    public ObjectWithId( String id ) {
       this.id = id;
    }

    public final String getId() {
       return id;
    }
}

And then you can extend it to declare different types of objects that have ids. Here you have a fully specified and implemented behaviour but no restriction on any other behaviours subclasses may exhibit.

Note though that a much neater way to model the same thing is to use composition instead of inheritance.

public final class ObjectWithId<T> {
    private final String id;
    private final T ob;

    public ObjectWithId( String id, T ob ) {
       this.id = id;
       this.ob = ob;
    }

    public String getId() {
       return id;
    }

    public T getObject() {
       return ob;
    }
}

But before generics were introduced (up to Java version 1.4), this wouldn't have been as elegant and obviously better than the abstract class solution because you'd have had to trade in type safety.

like image 109
biziclop Avatar answered Oct 05 '22 18:10

biziclop


  • you can declare to implement an interface and don't provide implementation and then each child implicitly gets interface extended

  • you prevent to create instance of this class

  • you in future provide common implementation to all children
like image 25
jmj Avatar answered Oct 05 '22 19:10

jmj