| 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: 3026
|
|
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 ...
|
 |
 |
|
|
subject: inheritance question
|
|
|