Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need abstract methods?

I have been studying abstract methods lately and I can't understand why do we need them? I mean, after all, we are just overriding them. Do you know its just a declaration? Why do we need them?

Also, I tried understanding this from the internet and everywhere there's an explanation like imagine there's an abstract class human then there're its subclasses disabled and not disabled then the abstract function in human class walking() will contain different body or code. Now what I am saying is why don't we just create a function in the disabled and not disabled subclasses instead of overriding. Thus again back to the question in the first paragraph. Please explain it.

like image 447
Mad Dawg Avatar asked Dec 10 '19 16:12

Mad Dawg


People also ask

What is the main reason for using abstract classes?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Why do we need abstract class without abstract method?

Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.

How to use an abstract class with abstract methods?

You can use an abstract class by inheriting it from another class and then provide implementations to the abstract methods in it If an abstract class doesn’t have any method implementation, it’s always better to use interface A class declared abstract may or may not include abstract methods. But, what exactly is an abstract method?

What are the advantages of using abstraction in programming?

The main benefit of using an Abstraction in Programming is that it allows you to group several related classes as siblings. Abstraction in Object Oriented Programming helps to reduce the complexity of the design and implementation process of software. When to use Abstract Methods & Abstract Class?

How many arguments can an abstract method have in Java?

It may have zero or more arguments. Following points are the important rules for abstract method in Java: An abstract method do not have a body (implementation), they just have a method signature (declaration). The class which extends the abstract class implements the abstract methods.

What is the difference between an interface and an abstract class?

I n C#, we have a very important class known as an abstract class. An abstract class can have one or more methods, which can be abstract (only signature). The interface only contains the method signature. How is an abstract class different from an interface in C#?


2 Answers

One of the most obvious uses of abstract methods is letting the abstract class call them from an implementation of other methods.

Here is an example:

class AbstractToy {
    protected abstract String getName();
    protected abstract String getSize();
    public String getDescription() {
        return "This is a really "+getSize()+" "+getName();
    }
}
class ToyBear extends AbstractToy {
    protected override String getName() { return "bear"; }
    protected override String getSize() { return "big"; }
}
class ToyPenguin extends AbstractToy {
    protected override String getName() { return "penguin"; }
    protected override String getSize() { return "tiny"; }
}

Note how AbstractToy's implementation of getDescription is able to call getName and getSize, even though the definitions are in the subclasses. This is an instance of a well-known design pattern called Template Method.

like image 142
Sergey Kalinichenko Avatar answered Oct 19 '22 12:10

Sergey Kalinichenko


The abstract method definition in a base type is a contract that guarantees that every concrete implementation of that type will have an implementation of that method.

Without it, the compiler wouldn't allow you to call that method on a reference of the base-type, because it couldn't guarantee that such a method will always be there.

So if you have

MyBaseClass x = getAnInstance();
x.doTheThing();

and MyBaseClass doesn't have a doTheThing method, then the compiler will tell you that it can't let you do that. By adding an abstract doTheThing method you guarantee that every concrete implementation that getAnInstance() can return has an implementation, which is good enough for the compiler, so it'll let you call that method.

Basically a more fundamental truth, that needs to be groked first is this:

You will have instances where the type of the variable is more general than the type of the value it holds. In simple cases you can just make the variable be the specific type:

MyDerivedClassA a = new MyDerivcedClassA();

In that case you could obviously call any method of MyDerivedClassA and wouldn't need any abstract methods in the base class.

But sometimes you want to do a thing with any MyBaseClass instance and you don't know what specific type it is:

public void doTheThingsForAll(Collection<? extends MyBaseClass> baseClassReferences) {
  for (MyBaseClass myBaseReference : baseClassReferences) {
    myBaseReference.doTheThing();
  }
}

If your MyBaseClass didn't have the doTheThing abstract method, then the compiler wouldn't let you do that.

like image 42
Joachim Sauer Avatar answered Oct 19 '22 13:10

Joachim Sauer