Abstract methods and overriding? [duplicate] - java

This question already has answers here:
What is the point of using abstract methods?
(7 answers)
What is the purpose of an abstract method?
(5 answers)
Abstract class and methods, Why?
(4 answers)
Closed 5 years ago.
I'm Currently reading Head First Java, I'm learning about abstract classes and methods, it's all making wonderful sense at the moment but one thing is puzzling me.
The book states that if declaring an abstract method in an abstract class then this method must be overridden by any of the sub-classes, which initially made sense when i learned that abstract methods have no body, my question then is this:
If an abstract method has no body and must also be overridden by any sub-classes, what is the point in declaring an abstract method in the first place, the programmer still has to create the overridden method in each and every sub-class, so why not just forget about the abstract method which has no body anyway, thus alleviating the need to override, it just seems like less code to type or if looking at it the other way more code to type??
Thanks.
D.

Related

What is Abstraction in Java i need to understand it properly? [duplicate]

This question already has answers here:
Understanding the purpose of Abstract Classes in Java
(8 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I understand basics of abstraction but still I have some uncertainties.
We cannot create obj of abstract class.
Abstract methods are declared in abstract class but defined in child class.
And we call abstract methods using obj of child class.
I don't get why we using abstract classes if we can do all the things through objects of the child class. We defined abstract methods in child class so it also means we declared it in child class too and we calling that method so it working like normal classes and child.
What is practical purpose of abstract classes?
from what I can see from your question you look at abstract classes as interfaces, but you could and should use them differently.
In an abstract class there could be not abstract methods, meaning a method where all of the child classes use the super class method, this way you can avoid duplicate code.
You can take a look here for an explenation : https://softwareengineering.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces

Why Override an Interface method? [duplicate]

This question already has answers here:
When do you use Java's #Override annotation and why?
(27 answers)
Should we #Override an interface's method implementation?
(15 answers)
Closed 4 years ago.
When extending an abstract class I understand why you would have to over ride some methods. But when it comes to interfaces I do not understand why it must be done. If all methods of an interface must be implemented, what is the use of overriding them?.
Im talking about the #Override annotation.
Using #Override is purely optional. You don't have to use it. If you do, using #Override when implementing an interface has more or less the same reasoning as using it when extending an abstract class. In a nutshell, it prevents stupid mistakes - if an interface is changed, it forces you to change your class accordingly, instead of just tacking on the "new" methods you suddenly need to implement.

when abstract class doesn't contain any abstract method [duplicate]

This question already has answers here:
Why use an abstract class without abstract methods?
(11 answers)
Closed 7 years ago.
I am still confused when abstract class doesn't contain any abstract method, what a purpose of it? why don't use regular class rather than abstract class if it doesn't contain any abstract method ? In fact, I was saw this situation is applied on java and libgdx library or perhaps for every library.
So, because this situation, I was thinking is it very important to know why use abstract class without abstract method rather than regular class.
When you make a class abstract (either with or without abstract methods), you are forcing the users of this class to create concrete sub-classes of it, since they can't instantiate it.
A user of an abstract class must create a concrete derived class.
This can be useful since it allows the author of an abstract class to introduce abstract functions at a later date. The amount of refactoring necessary at that time is then significantly reduced.

What is the reason to declare method only? (java example) [duplicate]

This question already has answers here:
What is an abstract class? [duplicate]
(13 answers)
Closed 8 years ago.
Why one sometimes writes
abstract void etwas();
codcodecode
void etwas()
{
return;
}
Instead of skipping the single declaration? In which cases is it necessary?
In the same class, this almost never happens.
It happens when you have an abstract class, which defines an abstract method. You do so when you want to make sure that anyone who extends this abstract class, will be forced to provide his own implementation for this abstract method. Otherwise a compile error occurs.

why we are declaring class as abstract even if it has no abstract methods [duplicate]

This question already has answers here:
Defining an abstract class without any abstract methods
(12 answers)
Use of an abstract class without any abstract methods
(7 answers)
Closed 9 years ago.
can any one please explain the scenario when A class may be declared abstract even if it has no abstract methods. i have tried in many websites but i did not found it.Thanks a lot.
Broadly, this would be because the class provides concrete implementations of functionality but should not itself be instantiated. For example, an AbstractWidget in a price calculator may not be suitable to directly instantiate, but it has concrete implementations of certain widget functionality like being rearranged. The expectation is that the subclass adds new methods altogether, or overrides the ones already declared.

Categories