This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "inheritance question" Watch "inheritance question" New topic
Author

inheritance question

jesse harris
Ranch Hand

Joined: Oct 02, 2000
Posts: 62
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

Joined: Nov 28, 2002
Posts: 6
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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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


Steve
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Giovanni Pelosi
Greenhorn

Joined: Nov 28, 2002
Posts: 6
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

Joined: Nov 28, 2002
Posts: 6
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 ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance question