Why can abstract methods only be declared in abstract classes?
Asked Answered
C

9

24

I understand that in abstract classes methods be both abstract, or not. But why can I not create an abstract method in a "normal", non-abstract class?

Thanks in advance for any explanation!

Chiropody answered 4/3, 2014 at 1:45 Comment(3)
Because if the class is not abstract, you can create an object that's an instance of the class, and then you can try to call that method for the object, and the method doesn't exist because it's abstract, and then what?Andres
this is a design question, you should think about need, and change (design patterns).Mckoy
@Andres thanks for the concise response with a clear "what-if"Chiropody
M
37

Abstract method basically says, that there is no implementation of the method and it needs to be implemented in a subclass. However if you had an abstract method in a non-abstract class, you could instantiate the class and get an object, that would have an unimplemented method, which you would be unable to call.

Motivation answered 4/3, 2014 at 1:49 Comment(0)
M
3

Having an abstract method prevents a class from being instantiated, thus making it a de-facto abstract class. Java insists on you declaring this fact explicitly for consistency: technically, Java compiler does not need this additional mark in order to decide if a class is abstract based on the presence of abstract methods, but since you may want to make a class abstract without making any of its methods abstract, requiring the declaration on the class was the way to go.

Macrophage answered 4/3, 2014 at 1:51 Comment(0)
M
3

Lets start by understanding why we need something like a abstract method. The answer is simple. I dont want my extenders to use my methods as it is, I want them to define their own behavior of a particular method. Since I use this method in other methods of my abstract class. I can provide a /**java doc**/ on the abstract class and point them to use a default behavior.

class abstract LoveTheWorld {
    private int myKindness ;
    public int defaultGiveKindness() {
        myKindness -= 5 ;
        return 5 ;
    }
    /**
    you can use the defaultGiveKindness method, and not define your own behavior
    **/
    public abstract int giveKindness() ;
}

This also tells the extender that they can extend only one class (as per java inheritance rules). Now, if you want to twist this story around, you can use interface instead of a abstract class. But it all depends on what constraints do you want your future developer to adhere to, strict or flexible. Strict will keep it tight and ensure reduced mistakes, flexible will keep it loose and free and promote innovation. The question is **what do you need*8.

Mckoy answered 4/3, 2014 at 2:1 Comment(0)
G
1

You do want to call a method providing an implementation, that's the essence of programming.

Based on this idea, Java rules are:

  • You cannot instantiate an abstract class.
  • You can only instantiate a non-abstract class.

What if Java let you call a method on an instance of a non-abstract class with...no implementation since method would be abstract => no sense at all.

That's why Java and any other languages dealing with similar mechanism like C# (virtual method) prevent to declare an abstract method in a non-abstract class.

Gilead answered 4/3, 2014 at 1:59 Comment(0)
C
1

If a concrete class could have an abstract method, you would not be able to create an object from it. Imagine a Shape with a concrete method getColor() and an abstract method draw(). It's great to refer to some things in an abstract manner, so someone can tell you "render a shape!":

public void render(Shape s) { ... }

But when you use that reference to call draw(), you expect that the object referred by s knows how to do that:

s.draw();

If concrete classes were allowed to have abstract methods, you could instantiate a Shape object, but when you called the draw method, it wouldn't know what to draw! Even if it knew how to say its color, position or 1000 other things. If it's not 100% specified, it can't exist as a working objects.

So Java requires that such classes be marked as abstract. Then you won't be able to use them to create objects, since they don't know how to concretely do 100% of the things that you expect from the object. You can only use abstract classes to refer to them. You can be sure now that only classes that have all their methods implemented will be used to create objects, and they will probably have names that seem less abstract too:

Shape shape = new Rectangle();
render(shape);

Now you can say "render the shape" and you program, using reference to the rectangle, will know how to draw() it.

Cagey answered 4/3, 2014 at 2:40 Comment(0)
B
0

Because having an abstract method makes it an abstract class. The reasoning is circular.

Ballad answered 4/3, 2014 at 1:51 Comment(0)
F
0

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.Then the problem will occur.That's is the reason abstract methods should be in abstract class (so that one cannot create the object for the abstract class)or interfaces only.

Fromm answered 4/3, 2014 at 2:7 Comment(0)
H
0

The simple answer is if the class is not abstract(concrete class) you could be able to instantiate that class and call any method of that class .but let's say if you had declared an abstract method in that non-abstract class-it's impossible to call that particular abstract method. (to prevent that, we can't declare abstract methods in non-abstract class )

Hofmann answered 27/1, 2018 at 8:22 Comment(0)
C
0

Looking at it the other way, if you are defining a method as abstract that means you are surely inheriting that class to some other class/classes otherwise no use of defining it as abstract. Moreover, you should keep abstract methods in those classes which are parent class and have members that are inheritable and characteristics to other sub-class. So if for one abstract method a whole class is need to be inherited then we could define the other variables too with required access specifiers so that it also get inherited as needed and hence no need to create objects of parent class to call concrete members.

Convolvulaceous answered 19/10, 2020 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.