1

What are the advantages/disadvantages of using one of the following styles over the other for declaring a method from an abstract parent class:

OPTION 1:

Parent Class:

protected Object retrieve(String id, Object model){
        return null;
}

Child Class:

@Override
public String retrieve(Object model) {
    if (model instanceof Car)
        // ... somehow get id ...
        return getInfo(id, (Car)model);
    return null;
}

or...

OPTION 2:

Parent Class:

protected abstract Object retrieve(String id, Object model);

Child Class:

public String retrieve(String id, Object model) {
    if (model instanceof Car)
        return getInfo(id, (Car)model);
    return null;
}
Fuzzy Analysis
  • 3,168
  • 2
  • 42
  • 66
  • 8
    These are not opposite methods; you're comparing apples and oranges here. You can still use `@Override` in the child method in Option 2. – Oliver Charlesworth Jan 03 '13 at 02:32
  • "abstract parent class" <-- uh? – fge Jan 03 '13 at 02:34
  • yeah by "abstract parent class" i meant that the 'Parent' method is inside a parent class that is declared abstract... just added that info to see if it mattered. Yes, I am a newb. – Fuzzy Analysis Jan 03 '13 at 02:36

4 Answers4

4

With an abstract class, it is guaranteed that the child will override the abstract method(s), if any (or itself be abstract). They can also not be instantiated, which is important because they have missing functionality (the abstract method).

See also http://wiki.answers.com/Q/Use_of_abstract_class_in_java:

An Abstract class is similar to an interface. You cannot instantiate them, but you can extend them. Any class that extends the abstract class has to provide the implementation to the abstract methods. Hence these classes can be used as a skeleton to similar classes where some common functionality may be required. Such functionality can also be embedded into these classes. Unlike interfaces, abstract classes can have method code also. So they are very useful.

This may also be helpful: https://softwareengineering.stackexchange.com/questions/96947/why-should-i-declare-a-class-as-an-abstract-class

Community
  • 1
  • 1
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Good answer, but the question is related to "abstract methods". – Usman Saleem Jan 03 '13 at 02:40
  • @UsmanSaleem no, it is asking `What are the advantages/disadvantages of using one of the following styles over the other for declaring a method from an abstract parent class` – tckmn Jan 03 '13 at 02:41
  • you can technically declare an abstract class without any abstract method. Subclasses are only 'forced' to override 'abstract' methods or declare themselves as abstract as well. – Usman Saleem Jan 03 '13 at 02:44
  • 1
    "With an abstract class, it is guaranteed that the child will override the abstract method(s)" <-- no. The only guarantee is that this class can never be instantiated, and that you must `extends` it. There is no obligation at all that there be `abstract` methods in this class at all. – fge Jan 03 '13 at 03:04
  • @fge http://i.imgur.com/cAZxV.png in fact, you have `MustHaveToString` as an example in your own answer. – tckmn Jan 03 '13 at 03:07
  • @fge *it is guaranteed that the child will override the abstract method(s)* – tckmn Jan 03 '13 at 03:08
  • @Doorknob _if_ there are such methods. You can declare methods as being `final` in an `abstract` class. You can even declare `static` methods in such a class (and all `static` methods are effectively `final`). Hence what I said: "there is no obligation at al that there be `abstract` methods in [an abstract] class at all". – fge Jan 03 '13 at 03:10
  • @fge But that statement is true. It is guaranteed that the child will override the abstract method(s). – tckmn Jan 03 '13 at 03:12
  • @Doorknob the statement should be "it is guaranteed that the child will override the abstract method(s), ** if any**". As I said, "There is no obligation at all that there be abstract methods in [an abstract class]" – fge Jan 03 '13 at 03:17
  • @fge okay then, doesn't seem too important because who would create an abstract class with no abstract methods? :P But I put it there anyway – tckmn Jan 03 '13 at 03:19
3

In my opinion, the first style is only needed if all of the subclasses wont be implementing this function (thought that might be better expressed with interfaces). If it is a necessary function, it is clearer to say abstract, and less error prone, since the compiler will nag you to implement it.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

You are mixing apples and oranges here, and probably a little more:

  • @Override is a compile-time (@Retention(value=SOURCE)), method-level (@Target(value=METHOD)) annotation, used by the compiler to check that the given method either:
    • implements a method from an interface (note: JDK 1.6+), or
    • implements an abstract method from an abstract class, or
    • overrides a non-final method from a parent class (whether that parent class is abstract or not);
  • abstract is a language keyword which can be declared at class level or method level:
    • if at class level, this means this class cannot be directly instantiated, and therefore you must subclass it (such an abstract class can, however, bear static factory methods returning subclasses of itself);
    • if at method level, the method only has a prototype (see below) and subclasses must implement this method;

Note that it is illegal to declare a method as being abstract if the class itself is not abstract.

Also note that this is possible:

public abstract class MustHaveToString
{
    // .toString() is implemented in Object, here we FORCE children of that class
    // to re-implement it. As the method is abstract, we only declare its 
    // prototype (its return type and method arguments, in this case, none).
    // As the method originates from one of our parents (Object), 
    // we can also @Override it!
    @Override
    public abstract String toString();
}
fge
  • 119,121
  • 33
  • 254
  • 329
  • Good points. I also found this info on @Override : http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why ... it sort of seems like it is a hack specifically for certain IDEs at compile-time and/or specific versions of Java? – Fuzzy Analysis Jan 03 '13 at 02:52
  • 1
    The fact is, the `@Override` annotation is _never_ required. However, since it is a compile-time annotation, misplacing it can lead to (useful) compiler errors. As to Eclipse, get it out of the picture here: javac, in itself, makes good use of this annotation already ;) Surely enough, static code analysis by IDEs can point to other suspicious code constructs which still compile correctly. – fge Jan 03 '13 at 02:59
1

First of all, your option '1' is an example of method overloading. Secondly, assuming that you meant method overriding, in option '1' you can always call 'super.method' from overridden method. This is technically not allowed in option '2'. So there are no 'advantages' or disadvantages' of these two techniques.

A more closer approach to your 'option 2' (without declaring 'abstract') is as follows:

public abstract class Parent {
  protected Object retrieve(String id, Object model) {
    throw new UnsupportedOperationException(...);
  }
}
Usman Saleem
  • 1,665
  • 9
  • 18