Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of using abstract methods?

What's the point of using "abstract methods"? An abstract class cannot be instantiated, but what about the abstract methods? Are they just here to say "you have to implement me", and if we forget them, the compiler throws an error?

Does it mean something else? I also read something about "we don't have to rewrite the same code", but in the abstract class, we only "declare" the abstract method, so we will have to rewrite the code in the child class.

Can you help me understand it a bit more? I checked the other topics about "abstract class/methods" but I didn't find an answer.

like image 321
Paul Avatar asked Dec 06 '11 01:12

Paul


People also ask

What is the point of abstract classes and methods?

Why And When To Use Abstract Classes and Methods? To achieve security - hide certain details and only show the important details of an object. Note: Abstraction can also be achieved with Interfaces, which you will learn more about in the next chapter.

What is the main benefit of abstraction?

Advantages of Abstraction It reduces the complexity of viewing things. Avoids code duplication and increases reusability. Helps to increase the security of an application or program as only essential details are provided to the user. It improves the maintainability of the application.


2 Answers

Say you have a three printers that you would need to write a driver for, Lexmark, Canon, and HP.

All three printers will have the print() and getSystemResource() methods.

However, only print() will be different for each printer. getSystemResource() remains the same throughout the three printers. You also have another concern, you would like to apply polymorphism.

So since getSystemResource() is the same for all three printers, so this can be pushed up to the super class to be implemented, in Java, this can be done via making this abstract in the super class, and in making a method abstract in a super class, the class itself needs to be abstract as well.

public abstract class Printer{   public void getSystemResource(){      // real implementation of getting system resources   }    public abstract void print(); }  public class Canon extends Printer{   public void print(){     // here you will provide the implementation of print pertaining to Canon   } }  public class HP extends Printer{   public void print(){     // here you will provide the implementation of print pertaining to HP   } }  public class Lexmark extends Printer{   public void print(){     // here you will provide the implementation of print pertaining to Lexmark   } } 

Notice that HP, Canon and Lexmark classes do not provide the implementation of getSystemResource().

Finally, in your main class, you can do the following:

public static void main(String args[]){   Printer printer = new HP();   printer.getSystemResource();   printer.print(); } 
like image 117
Oh Chin Boon Avatar answered Sep 30 '22 17:09

Oh Chin Boon


Besides the reminder that you have to implement it, the big advantage is that anyone who references the object by its abstract class type (including this in the abstract class itself) can use the method.

For instance, let's say we have a class responsible for taking state and manipulating it in some way. The abstract class is going to be responsible for getting the input, converting it to a long (for instance) and combining that value with the previous value in some way -- that "some way" is the abstract method. The abstract class may look something like:

public abstract class StateAccumulator {     protected abstract long accumulate(long oldState, long newState);      public handleInput(SomeInputObject input) {         long inputLong = input.getLong();         state = accumulate(state, inputLong);     }      private long state = SOME_INITIAL_STATE; } 

Now you can define an addition accumulator:

public class AdditionAccumulator extends StateAccumulator {     @Override     protected long accumulate(long oldState, long newState) {         return oldState + newState;     } } 

Without that abstract method, the base class would have no way to say "handle this state somehow." We don't want to provide a default implementation in the base class, though, because it wouldn't mean much -- how do you define a default implementation for "someone else will implement this"?

Note that there's more than one way to skin a cat. The strategy pattern would involve declaring an interface that declares the accumulate pattern, and passing an instance of that interface to the no-longer-abstract base class. In lingo terms, that's using composition instead of inheritance (you've composed an addition aggregator out of two objects, an aggregator and an adder).

like image 22
yshavit Avatar answered Sep 30 '22 17:09

yshavit