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();
}
}
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();
}
}