Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance question

 
jesse harris
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have multiple classes that are comprised with static methods and static variables. The methods in all of these classes are exactly the same, so creating a superclass with all of those methods and variables seemed like a good choice, but the difference in the sublasses are the values of those variables. Here is my question, if I overide, or "hide" the varibles in the subclass will the methods that I call from my subclass use the values in my subclass of the superclass?
note: the methods in the superclass directly access the varibles.
thanks a lot
Jesse
 
Giovanni Pelosi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhm, i found static variable inheritance an odd topic !
if you have:
class A {static String x;}
class B extends A {}
you only have a single x variable
A.x == B.x
good if you need a single global variable
for instance of class A and its inherited types
but in this way you cannot "extend" class with
class "attibutes"
(something like smalltalk "metaclass")
suppose, for instance
class A {
static String classAuthor;
static { classAuthor = "someone"; }
}
class B extends A {
static { classAuthor = "someoneelse"; }
}
is meaningless
where to put class attributes ?
maybe outside,
with a TypeInfo descriptor,
stored in a global hashmap,
indexed by Class or ClassName ...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are exactly the same, then they should be part of the same class. But I dont see why they should be static? do you have a good reason for making them static?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the question is this:
Class A {
public static author;
}
Class B extends A{
public static author = "SomeOne";
}
Class C extends A{
public static author = "SomeOneElse";
}
Only reason to have it in A is so methods in A know it exists, and classes cast back to A can still reference it. Is this a good way of doing it?
Steve
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
I think the question is this:
Class A {
public static author;
}
Class B extends A{
public static author = "SomeOne";
}
Class C extends A{
public static author = "SomeOneElse";
}
Only reason to have it in A is so methods in A know it exists, and classes cast back to A can still reference it. Is this a good way of doing it?
Steve

This doesn't work, as fields aren't polymorphic (as methods are). Casting to A will give you a null value for author, whatever the value in the subclass will be.
The way to do this is to define a (non-static) getter method for the author.
 
Giovanni Pelosi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the only way to extend class "meta" attributes
is to put them in a global repository
class Repository {
static public Map map = new HashMap();
static TypeInfo getInfo(Class class) {
return (TypeInfo) map.get(class);
}
}
class TypeInfo {
public Class typeClass;
public String author;
TypeInfo() {
init();
Repository.map.add(typeClass,this); //or getName()
}
protected abstract void init();
}
class A {
protected static class AType extends TypeInfo()
static public TypeInfo TYPE = new TypeInfo {
protected void init() {
typeClass = A.class;
author = "someone";
}
}
}
System.println("A Class author:"+
Repository.getInfo(A.class).author);
 
Giovanni Pelosi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this schema could be extended further to the notion of
metaclass variables (Delphi Object Pascal like),
that supports the notion of static check
of class inheritance ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic