Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract method use vs regular methods

I would like to know the difference between two conventions:

  1. Creating an abstract base class with an abstract method which will be implemented later on the derived classes.
  2. Creating an abstract base class without abstract methods
    but adding the relevant method later on the level of the derived classes.

What is the difference?

like image 368
Mulder Avatar asked Apr 08 '11 22:04

Mulder


People also ask

When should abstract methods be used?

If both the Java interface and Java abstract class are used to achieve abstraction, when should an interface and abstract class be used? An abstract class is used when the user needs to define a template for a group of subclasses. An interface is used when a user needs to define a role for other classes.

What are the advantages of abstract method in Java?

The abstract class in Java enables the best way to execute the process of data abstraction by providing the developers with the option of hiding the code implementation. It also presents the end-user with a template that explains the methods involved.

Are abstract methods necessary?

It's not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn't declare any abstract methods. If abstract class doesn't have any method implementation, its better to use interface because java doesn't support multiple class inheritance.

Can we use abstract method in normal class?

abstract methods are meant for leaving the implementation to the child classes. If the normal class contains abstract method,then one can creating the object for that class and may call the abstract method just like normal method.


3 Answers

Much like interfaces, abstract classes are designed to express a set of known operations for your types. Unlike interfaces however, abstract classes allow you to implement common/shared functionality that may be used by any derived type. E.g.:

public abstract class LoggerBase
{
  public abstract void Write(object item);

  protected virtual object FormatObject(object item)
  {
    return item;
  }
}

In this really basic example above, I've essentially done two things:

  1. Defined a contract that my derived types will conform to.
  2. Provides some default functionality that could be overriden if required.

Given that I know that any derived type of LoggerBase will have a Write method, I can call that. The equivalent of the above as an interface could be:

public interface ILogger
{
  void Write(object item);
}

As an abstract class, I can provide an additional service FormatObject which can optionally be overriden, say if I was writing a ConsoleLogger, e.g.:

public class ConsoleLogger : LoggerBase
{
  public override void Write(object item)
  {
    Console.WriteLine(FormatObject(item));
  }
}

By marking the FormatObject method as virtual, it means I can provide a shared implementation. I can also override it:

public class ConsoleLogger : LoggerBase
{
  public override void Write(object item)
  {
    Console.WriteLine(FormatObject(item));
  }

  protected override object FormatObject(object item)
  {
    return item.ToString().ToUpper();
  }
}

So, the key parts are:

  1. abstract classes must be inherited.
  2. abstract methods must be implemented in derived types.
  3. virtual methods can be overriden in derived types.

In the second scenario, because you wouldn't be adding the functionality to the abstract base class, you couldn't call that method when dealing with an instance of the base class directly. E.g., if I implemented ConsoleLogger.WriteSomethingElse, I couldn't call it from LoggerBase.WriteSomethingElse.

like image 146
Matthew Abbott Avatar answered Oct 21 '22 08:10

Matthew Abbott


The idea of putting abstract methods in a base class and then implementing them in subclasses is that you can then use the parent type instead of any specific subclass. For example say you want to sort an array. You can define the base class to be something like

abstract class Sorter {
    public abstract Array sort(Array arr);
}

Then you can implement various algorithms such as quicksort, mergesort, heapsort in subclasses.

class QuickSorter {
    public Array sort(Array arr) { ... }
}

class MergeSorter {
    public Array sort(Array arr) { ... }
}

You create a sorting object by choosing an algorithm,

Sorter sorter = QuickSorter();

Now you can pass sorter around, without exposing the fact that under the hood it's a quicksort. To sort an array you say

Array sortedArray = sorter.sort(someArray);

In this way the details of the implementation (which algorithm you use) are decoupled from the interface to the object (the fact that it sorts an array).

One concrete advantage is that if at some point you want a different sorting algorithm then you can change QuickSort() to say MergeSort in this single line, without having to change it anywhere else. If you don't include a sort() method in the parent, you have to downcast to QuickSorter whenever calling sort(), and then changing the algorithm will be more difficult.

like image 43
Guy Gur-Ari Avatar answered Oct 21 '22 08:10

Guy Gur-Ari


In the case 1) you can access those methods from the abstract base type without knowing the exact type (abstract methods are virtual methods).

The point of the abstract classes is usually to define some contract on the base class which is then implemented by the dervied classes (and in this context it is important to recognize that interfaces are sort of "pure abstract classes").

like image 2
Lucero Avatar answered Oct 21 '22 10:10

Lucero