+ 2

Why can't we make objects of abstract Class?

15th Aug 2017, 11:25 AM
Krunal Gediya
Krunal Gediya - avatar
2 odpowiedzi
+ 3
That's the point. Make a class abstract, you can't instantiate it. It's a bit like saying "why can't we access private methods" or even "why can't I store a double inside a string". In programming, we like to move as much dumb work to the compiler as possible, so us programmers can focus on the important things. If you make a method private it's probably for a good reason, and if we had to make sure that no outside object calls a method, by hand, every single time, it would make our programming lives much harder. So we move it to the compiler and the compiler makes sure that nobody touches our private methods, and if something tries to, it prints an error. The same goes for storing doubles in a string - nothing is stopping us from doing that in theory (for the computer everything is just numbers!), but you'd never want to do that, so we tell the compiler to make sure nobody tries to. And for abstract classes it's the same, too; if you mark a class abstract it's probably for a good reason: some of the classes methods are missing. And by marking a class abstract the compiler keeps track for us that nobody tries to make an object from it, until subclasses have implemented the missing methods.
15th Aug 2017, 1:43 PM
Schindlabua
Schindlabua - avatar
+ 2
well, same reason as to why you can't make objects out of interfaces. There are abstract methods for subclasses to implement.
15th Aug 2017, 11:45 AM
Gao Xiangshuai
Gao Xiangshuai - avatar