badmad
badmad

Reputation: 2059

Why is it mandatory for a class to be abstract when it has atleast one abstract method?

What is the reason that I can't create a concrete class with abstract methods in it?

Is it enforced just to make sure that no object is created without abstract method definition? or is there another plausible reason for this restriction?

Upvotes: 0

Views: 565

Answers (4)

Because when you create an Abstract class you are in the middle of your abstractions level. i Mean you have some questions about class responsibilities or this class must to do something but they dont care how, partially. If you dont wanna have an implemented method, you must to create an interface. In my opinion the answer is in the class responsibilities and abstraction and not in the technology scope.

Upvotes: 0

Aragorn
Aragorn

Reputation: 5289

You are right, the reason is to prevent creating an object with no implementation for a method or more.

Upvotes: 0

Arne
Arne

Reputation: 8481

abstract class in the java constext is defined as the class has at least one abstract method. And an abstract method is just a not implemented method. This is a design decision that was just copied from c++ where it is exactly the same. The only difference is, that in c++ you do not need to tell the compiler that a class is abstract, the compiler knows it even without you telling it. Why this design decision was made in c++ I can't tell you, but having it eleminates a complete class of errors. The error that a method of a class get's called, when the method is not implemented in that sub class.

Upvotes: 0

Michael
Michael

Reputation: 2773

An abstract class is, by definition, incomplete. Therefore, you should not be able to instantiate abstract classes. An interesting side effect of this definition is that you can create abstract classes that have all concrete methods. It's just that you think that your class is incomplete and shouldn't be able to be instantiated.

Upvotes: 1

Related Questions