0

What exactly is the whole concept behind having an abstract class (in my case i'm referring to java). I understand the fact that it is too general to be implemented itself and therefore isn't useful. But surely just declaring it non-abstract and using the same inheritance that would be used if it were an abstract class would give all the same results.

Is the main advantage to save time but declaring abstract methods in the abstract superclass that have no method body and then overriding them in their subclasses?

  • most of what you are asking is explained here: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – kmera Jan 18 '14 at 16:58

4 Answers4

1

Suppose your base class has a method that must be overridden in any useful subclass. If you implement the method in the base class, and it is not overridden, the error will not be found until your dummy method body gets run. If you make the method and class abstract, the error will be found at compile time.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • yeah so its sort of like a safety mechanism? Do all methods in an abstract class need to abstract, i.e. named as abstract? –  Jan 18 '14 at 16:43
  • 1
    no only the ones you want to be abstract – kmera Jan 18 '14 at 16:53
  • and as soon as you have one abstract method, you need to declare the class as abstract too – kmera Jan 18 '14 at 16:54
  • I recommend reading [this](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) about abstract classes and methods – kmera Jan 18 '14 at 16:55
  • I think it also adds readability. Declaring a method as abstract and not providing a method body is a far clearer expression of non-implementation than providing a dummy method with comments indicating it must be overridden. – Patricia Shanahan Jan 18 '14 at 17:14
1

The main advantage is that you can't instantiate an abstract class. You might have a class Document like this:

public class Document {
    public void write(File file) {
        // I have no idea how to implement this
    }
    public void read(File file) {
        // I don't know here either
    }
}

You want people to make subclasses for their types of documents:

public class MyDocument extends Document {
    public void write(File file) {
        // Now I know how to implement
    }
    public void read(File file) {
        // this also can be implemented
    }
}

But you don't want people making new Documents:

Document doc = new Document(); // this should not be allowed

and you want people to do something like this:

Document doc = new MyDocument();

The solution is to make Document abstract:

public abstract class Document

and to make the methods abstract too:

public abstract void write(File file);
public abstract void read(File file);

Then you can't instantiate Document but you can instantiate subclasses. Subclasses must override read and write.

tbodt
  • 16,609
  • 6
  • 58
  • 83
0

You make a class abstract when you dont want to create an object of it and the common methods of the subclasses can be implemented in abstract super class and the other purpose is that you make the method abstract so that the sub class MUST implement the super class abstract methods to write own implementation. As simple as that.

praveen_mohan
  • 455
  • 2
  • 7
  • 15
0

Think of abstract classes as a way to create a contract in a API.

Assume you are creating a program that has a list of items that a store sells. Now this list will of course contain items, that will have certain common characteristics and certain individual characteristics. However we cannot sell an "item" - We want to sell a "TV" or a "Table".

So you would create an abstract class "Item", that has a price tag, manufacturing date etc. However you never want anyone to create an instance of class Item, because an item can be anything.

You want the programmers to subclass and create their own classes of type Item, that fullfills the contract that Item requires. In turn the API allows you to add classes that subclass item, to be put on a stores list of available items. This will also automatically offer functionality to these class that you do not need to think about when subclassing, as well as requiring you to override methods that you MUST implement for the class.

In Java, methods does not need to be abstract, in an abstract class, and can be implemented just like in normal classes. E.g. if you have a orice tag, you can implement a getter for this price tag - no need to force the subclass to implement this.

Hope it makes sense. Else feel free to comment so I can elaborate.

Dyrborg
  • 877
  • 7
  • 16
  • I think im begining to get it thanks, is it basically for control as in to try and control what methods get implemented etc, as well as not to waste time writing methods for a superclass that is so general that it generally has no use. Also can i ask if most superclasses in OOP would generally be abstract? It seems as if that would be the most sensible thing to do as most things that are able to have many subclasses are usually very general? –  Jan 18 '14 at 17:04
  • Abstract, by its very definition, is about generality. if I tell you I have an animal pet, you will only really know that I have a pet, but no idea which. If I say I have a dog then we are getting more specific, but you can only imagine what KIND of pet I have. If I tell you I have a Corgi you suddenly know EXACTLY what pet I have. I Java Animal might be declared an interface, and dog might be an abstract class, where Corgi is a normal class. – Dyrborg Jan 18 '14 at 19:22
  • Why is this interesting? Well my apartment might not allow animal pets, thus ruling out ANY kind of animal, or it might allow cats but not dogs, using certain types of animals but not others. Abstract types and generalities is used to specify rules and contracts. – Dyrborg Jan 18 '14 at 19:24
  • As for the interface vs abstract part: We can say that animals in general need to eat and reproduce. How they do this is very specific to what animal we are talking about, and as such cannot say how (e.g. these methods in Java, can by their very definition not be implemented). However we might implement these for dogs, as their food and the way the reproduce is more or less the same across all dog species. – Dyrborg Jan 18 '14 at 19:27