0

I know it's a noob question, but I must ask it nonetheless: what is the purpose of using an abstract class?

Let's say I want to extract data from 10 different gambling websites. So I create an abstract class with abstract methods and I override those methods in each of my 10 classes (1 for each website - since each website has a different DOM).

The method in the abstract class does nothing really. Why can't I just declare a method with the same name to each one of my 10 classes?

Same question for Interface.

Rotem Orbach
  • 169
  • 1
  • 2
  • 10
  • Note the downvotes are not about this being a bad question, but a duplicate question. I think it is a very good question, but you should check whether the question has been asked *before* asking it. Now, you should check the referred duplicates. – JMCF125 Dec 28 '13 at 12:26

4 Answers4

2

It's vital for a concept in OOP (and other programming paradigms as well) called polymorphism. In your example, it allows you to say something all those objects have in common, and while the implementation is specific to each class, it says all of them should have it, that's something that characterizes them.

Polymorphism allows one to generalize anything in common, and often may avoid boilerplate code by giving a higher level of abstraction. Quoting from Wikipedia:

In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape") is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type, or types. There are several fundamentally different kinds of polymorphism:

  • If a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations, it is called ad hoc polymorphism. Ad hoc polymorphism is supported in many languages using function overloading.

  • If the code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism. In the object-oriented programming community, this is often known as generics or generic programming. In the functional programming community, this is often simply called polymorphism.

  • Subtyping (or inclusion polymorphism) is a concept wherein a name may denote instances of many different classes as long as they are related by some common superclass. In object-oriented programming, this is often referred to simply as polymorphism.

Community
  • 1
  • 1
JMCF125
  • 378
  • 3
  • 18
1

It gives your program structure. You can maybe have an abstract base class called BaseScraper and then have derived classes for each site you want to scrape from each with a method called scrape(). You could then add an instance of each scraper to an array and iterate through this array calling Scrape() on each instance to gather data. If you wanted to add another site you would just need to create another derived class that implements the Scrape() method and add it to your array of scrapers. You would not need to change any of your other code, the new scraper would then be incorporated automatically.

Damo
  • 361
  • 4
  • 16
1

Your example does not fit the real purpose of abstract classes. Try to read some like this:

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Cappelli
  • 138
  • 1
  • 9
1

There are several reasons for the different approaches. Consider the following:

public abstract class MyAbstractBaseClass
{
     String m_url;
     public MyAbstractClass(String url)
     {
         m_url = url;
     }

     public abstract String GetSiteData();
}

public class SiteAGetter : MyAbstractBaseClass
{
     // ...
     public override String GetSiteData()
     {
     // ...
     }
}

With this abstract method, you are forced by the compiler to really implement the GetSiteData() method in every overriding implementation. You could, of course, just implement the GetSiteData in the base class as

public virtual String GetSiteData()
{
     return null;
}

But now you don't need to implement the method in derived classes and you would need to make sure at every place where you call the method that the return value is non-null before you use it. So basically it is a help to yourself to prevent any obvious errors (you do not want anyone to write a derived class which does not implement that method). Also, an abstract class cannot directly be instantiated, so no one will make the mistake of creating an instance of the base class, because the compiler prevents it.

Interfaces serve a slightly different purpose: They can be implemented by different, unrelated classes. So if for some reason you cannot create a common base, but you want several classes to expose the same interface (= to offer the same functionality) but they are not related, use an interface. Interfaces are also used for other reasons, such as Mocking in unit test environments. There's even more to this, I suggest you read some good book about it and then ask more specific questions.

PMF
  • 14,535
  • 3
  • 23
  • 49