I have a some simple Java code that looks similar to this in its structure:
abstract public class BaseClass {
String someString;
public BaseClass(Str
It is possible but not the way you have it.
You have to add a no-args constructor to the base class and that's it!
public abstract class A {
private String name;
public A(){
this.name = getName();
}
public abstract String getName();
public String toString(){
return "simple class name: " + this.getClass().getSimpleName() + " name:\"" + this.name + "\"";
}
}
class B extends A {
public String getName(){
return "my name is B";
}
public static void main( String [] args ) {
System.out.println( new C() );
}
}
class C extends A {
public String getName() {
return "Zee";
}
}
When you don't add a constructor ( any ) to a class the compiler add the default no arg contructor for you.
When the defualt no arg calls to super(); and since you don't have it in the super class you get that error message.
That's about the question it self.
Now, expanding the answer:
Are you aware that creating a subclass ( behavior ) to specify different a different value ( data ) makes no sense??!!! I hope you do.
If the only thing that is changes is the "name" then a single class parametrized is enough!
So you don't need this:
MyClass a = new A("A");
MyClass b = new B("B");
MyClass c = new C("C");
MyClass d = new D("D");
or
MyClass a = new A(); // internally setting "A" "B", "C" etc.
MyClass b = new B();
MyClass c = new C();
MyClass d = new D();
When you can write this:
MyClass a = new MyClass("A");
MyClass b = new MyClass("B");
MyClass c = new MyClass("C");
MyClass d = new MyClass("D");
If I were to change the method signature of the BaseClass constructor, I would have to change all the subclasses.
Well that's why inheritance is the artifact that creates HIGH coupling, which is undesirable in OO systems. It should be avoided and perhaps replaced with composition.
Think if you really really need them as subclass. That's why you see very often interfaces used insted:
public interface NameAware {
public String getName();
}
class A implements NameAware ...
class B implements NameAware ...
class C ... etc.
Here B and C could have inherited from A which would have created a very HIGH coupling among them, by using interfaces the coupling is reduced, if A decides it will no longer be "NameAware" the other classes won't broke.
Of course, if you want to reuse behavior this won't work.
You can solve this error by adding an argumentless constructor to the base class (as shown below).
Cheers.
abstract public class BaseClass {
// ADD AN ARGUMENTLESS CONSTRUCTOR TO THE BASE CLASS
public BaseClass(){
}
String someString;
public BaseClass(String someString) {
this.someString = someString;
}
abstract public String getName();
}
public class ACSubClass extends BaseClass {
public ASubClass(String someString) {
super(someString);
}
public String getName() {
return "name value for ASubClass";
}
}
Sorry for necroposting but faced this problem just today. For everybody also facing with this problem - one of he possible reasons - you don't call super
at the first line of method. Second, third and other lines fire this error. Call of super should be very first call in your method. In this case everything is well.
I had this error and fixed it by removing a thrown exception from beside the method to a try/catch block
For example: FROM:
public static HashMap<String, String> getMap() throws SQLException
{
}
TO:
public static Hashmap<String,String> getMap()
{
try{
}catch(SQLException)
{
}
}