aspose file tools*
The moose likes Beginning Java and the fly likes Abstract class with non-static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class with non-static variables" Watch "Abstract class with non-static variables" New topic
Author

Abstract class with non-static variables

harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
I tried declaring a non-static variable in an abstract class. The compiler did not complain. My concern is what can we do with a non-static variable inside an abstract class? You can not instantiate an abstract class anyways to access it. Alternatively think that you may use that variable in a non-abstract method in the same class. But again there is no way you can access that non-static non-abstract class because you can not instantiate an object. My question here is ... Do we see any logic in creating a non-static variable inside an abstract class?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
harsha balluru wrote:I tried declaring a non-static variable in an abstract class. The compiler did not complain. My concern is what can we do with a non-static variable inside an abstract class? You can not instantiate an abstract class anyways to access it. Alternatively think that you may use that variable in a non-abstract method in the same class. But again there is no way you can access that non-static non-abstract class because you can not instantiate an object. My question here is ... Do we see any logic in creating a non-static variable inside an abstract class?


sure! If you want to implement a subclass of the abstract class, and you can instantiate the subclass. then the subclass can inherit these nonstatic variables you are talking about.
harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
Oh..Thanks Fred.. I could not think that deeper. I see what you are saying.

But again if the non abstract subclass has a static variable with the same name, then the superclass' non-static variable value will be overridden correct?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

But again if the non abstract subclass has a static variable with the same name, then the superclass' non-static variable value will be overridden correct?


Try it. It is a simple test to see if it compiles.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
It compiles.... Don't you think compiler should warn about this overriding where you will make the superclass' variable unusable.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

harsha balluru wrote:It compiles.... Don't you think compiler should warn about this overriding where you will make the superclass' variable unusable.


Hint: Does polymorphism apply to variables?

Henry
harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
Thanks..I understand now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class with non-static variables