Why can't we instantiate a abstract class in J

2020-02-07 19:38发布

I understand:

  1. Since Abstract class are nothing on its own i.e. vehicle that's why we want to create an object of an concrete implementation like Car,Bike etc.
  2. Constructor of an Abstract class gets called during object chaining.

  3. We can never directly create an object of Abstract class even if it contains a Constructor and all methods are implemented

why ? I am looking to understand from Compiler perspective, why Java forces these ?

Thanks

15条回答
来,给爷笑一个
2楼-- · 2020-02-07 20:16

abstract it self tells : existing in thought or as an idea but not having a physical or concrete existence. In java term , abstract keyword definition , if abstract comes before a class name, then JDK tells to JVM about discard that class object initiation. It's correct, abstract class can have multiple constructor but only for constructor chaining.

查看更多
3楼-- · 2020-02-07 20:17

rocketboy shows some mechanistic reasons, but there's a conceptual reason.

An Abstract class represents an abstract concept. Take your vehicle example. You cannot build a vehicle that is not something more specific. You can have a set of vehicles, that could be made of 2004 corolla's and '98 ford escorts and 1984 cs36 (a kind of yacht), a mark 4 firefly class mid-range bulk transport(the one with the stabilizers), you can take any one of those individually and call them a vehicle but you cannot have something that is only a vehicle and not one of those or some other specific type of vehicle.

Abstract classes represent such abstract concepts as vehicle. Hence the idea of instantiating one is non-sensical because to actually instantiate it you need to know what you're instantiating.

查看更多
迷人小祖宗
4楼-- · 2020-02-07 20:18

The reason why Java doesnt allows an abstract class to be instantiated is logical. We haven't given the definition of a method and therefore, if it had allowd the creation of the object, there was no return address to pop the function from the stack and we get thus, stuck. Thus, its logical only not to allow the object instattion.

查看更多
等我变得足够好
5楼-- · 2020-02-07 20:20

An abstract class is not complete! The author marked it abstract to tell you that some implementation is missing in the code. The author has done some of the work, but you must fill in some bits yourself to get it working. The abstract keyword ensures that no one would accidentally initiate this incomplete class.

Think of repairing a car. Someone has removed the brake pads and is going to replace them in the next day. Now, to prevent someone accidentally driving this car(which has no brakes installed), the mechanic installs a lock on the steering wheel. It's a fail-safe measure.

查看更多
狗以群分
6楼-- · 2020-02-07 20:20

Because Java restricted it that's why we can not instantiated the abstract class. Because in general scenario abstract means incomplete so we can not make of object of incomplete things.We have to provide the complete implementation of an abstract class in a concrete class. But we cannot create the object of an abstract class.

查看更多
闹够了就滚
7楼-- · 2020-02-07 20:22

Because an Abstract Class is a skeleton structure(an incomplete construct if you may), hence the term Abstract.

abstract class Person(){
   abstract void Speak();
}

Means every Person must speak. That means every person should know how to speak (implement the speak()). new Person() cannot have that, so it is not allowed.

查看更多
登录 后发表回答