• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Abstract Classes and Abstract Methods!

 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic