• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Abstract Classes and Abstract Methods!  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this program:


Two queries regarding this:
1. Why does the compiler allow me to access "method" eventhough it is declared abstract?

And also if I make "variable" a local variable inside main() I get an error that says "variable" has not been initialized. Now if "variable" is made an instance variable like in the above program it must be getting a default value otherwise the compiler would've complained.

2.What is that default value?

Thanks in advance for any help!
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:
And also if I make "variable" a local variable inside main() I get an error that says "variable" has not been initialized. Now if "variable" is made an instance variable like in the above program it must be getting a default value otherwise the compiler would've complained.

2.What is that default value?


All class members are initialized in java. All primitive types are initialized to zero and all reference types to null.

But no local variables are initialized in java. That's why you get the error when "variable" is local.
 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:
1. Why does the compiler allow me to access "method" eventhough it is declared abstract?


You cannot create an instance of an abstract class, only from it's subclass, that subclass would need to implement the abstract method (or itself be declared abstract). Therefore the compiler allows you to acces an abstract method knowing that any object you will call it on, will have a concrete implementation.
In the code you have written the JVM will throw a NullPointerException as you have not initialized variable.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!