sort of abstract attribute

V

VisionSet

Seems to me it would make sense to be able to do this:

class MySuper {

String myString;
// or this: abstract String myString; // illegal

void mySuperMethod() {System.out.println(myString);}
}
class MySub {

myString = "StringInMySub"; // illegal

public static void main(String[] args) {
MySuper myInst = new MySub();
myInst.mySuperMethod();
}
}

Instead I have to do something like this:

abstract class MySuper {

abstract String myAttributeReturner();

void mySuperMethod() {System.out.println(myAttributeReturner());}

}
class MySub {

String myString = "StringInMySub";

String myAttributeReturner() {return myString;}

public static void main(String[] args) {
MySuper myInst = new MySub();
myInst.mySuperMethod();
}
}

please put my absurd thinking straight ;-)
Is the 2nd approach the usual way to handle this?
Obviously I need to handle a number of subclasses common abilities through
their superclass.

another alternative is this:

abstract class MySuper {

String myString;

void mySuperMethod() {System.out.println(myString);}
}
class MySub {

private static final String MY_STRING = "StringInMySub";

MySub() {myAttributeCopier();}

String myAttributeCopier() {myString = MY_STRING;}

public static void main(String[] args) {
MySuper myInst = new MySub();
myInst.mySuperMethod();
}
}
 
V

VisionSet

Noé R. Barranco said:
Or, if what you really want is to force common behaviours amongst a group of
classes,
you could also try using interfaces which are quite versatile for this
purpose.

I don't see how interfaces can really help, they can only have 'constant'
attributes.

I want to reference attributes in a common way in my abstract superclass in
the same way I can with methods.
My guess is the simplest approach is to call abstract methods that when
implemented return what I want.
 
N

Noé R. Barranco

VisionSet said:
group

I don't see how interfaces can really help, they can only have 'constant'
attributes.

I want to reference attributes in a common way in my abstract superclass in
the same way I can with methods.
My guess is the simplest approach is to call abstract methods that when
implemented return what I want.
Seems like I'm missing the point....what goal are you trying to reach by
managing
your subclasses attributes from the superclass ? Why can't you just invoke
the overriden
method(s) directly from the subclass ?

Noé B.

Noé
 
X

xarax

VisionSet said:
Seems to me it would make sense to be able to do this:

class MySuper {

String myString;
// or this: abstract String myString; // illegal

void mySuperMethod() {System.out.println(myString);}
}
class MySub {

myString = "StringInMySub"; // illegal

public static void main(String[] args) {
MySuper myInst = new MySub();
myInst.mySuperMethod();
}
}
/snip/

1. Be sure MySub extends MySuper.

2. In MySub, put the string initializer inside an
initializer block.

class MySub
extends MySuper
{
// instance initializer
{
myString = "StringInMySub";
}

// other stuff
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top