Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class with non-static variables

 
harsha balluru
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
harsha balluru
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles.... Don't you think compiler should warn about this overriding where you will make the superclass' variable unusable.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..I understand now...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic