Java: why does an extension need an empty constructor?

I have SearchToUser and getFilesToWord classes. GetFilesToWord must inherit SearchToUser fields. The extension works if an empty construct is in the SearchToUser class, otherwise:

cannot find symbol symbol : constructor SearchToUser() location: class SearchToUser public class GetFilesToWord extends SearchToUser{ ^ 1 error make: *** [all] Error 1 

I cannot understand why an empty constructor is required for the extension.

[Added]

- ALERT ERRR! USE THE COMPOSITION! Left as a β€œbad” example - Composition VS Inheritance

It works, but can you find some flaws? Can I make searchTerm private, create a public method for it, create a SearchToUser object for a parameter in GetFilesToWord?

SearchToUser.java

 public class SearchToUser { public static GetFilesToWord geader; public static String searchTerm; SearchToUser(String s){ searchTerm=s.trim().toLowerCase(); files=geader.getFilesToWord(s); } ... } 

GetFilesToWord.java

 public class GetFilesToWord extends SearhToUser{ public GetFilesToWord(){super(SearchToUser.searchTerm){ ... } 
+4
source share
2 answers

The superclass does not need an empty constructor. A subclass just needs to call the constructor in the superclass. If the superclass has an open or protected no-arg constructor, this is called automatically, otherwise you need to be explicit.

Default constructor

 public class Super { } public class Sub extends Super { } 

Here Super does not specify a constructor, so it is added. Same thing for Sub . The above really looks like this:

 public class Super { public Super() { } } public class Sub extends Super { public Sub() { super(); } } 

Explicit no-arg constructor

 public class Super { public Super() { } } public class Sub extends Super { } 

It is legal. The constructor is added to Sub , which calls the default constructor Super .

Explicit constructor with arguments

 public class Super { public Super(int i) { } } public class Sub extends Super { } 

This is a compilation error like yours. Since Super has a constructor, the no-arg constructor is not automatically added by the compiler. A way to handle this:

 public class Super { public Super(int i) { } } public class Sub extends Super { public Sub() { super(0); // <-- explicit constructor call } } 
+16
source

Technically, this is not so ... but unless you explicitly provide a constructor in GetFilesToWord that explicitly calls the superclass constructor, Java will automatically add a call to the superclass constructor without arguments. But if there is no superclass constructor without arguments, Java does not know what values ​​to provide for the argument, and it cannot automatically insert a call. Then you need to manually call the constructor of the superclass with any values ​​that are appropriate.

The reason the superclass constructor should be called is because, in my opinion, the GetFilesToWord build operation involves the work of building a SearchToUser . For example, if you had instance variables in SearchToUser , you would need to initialize them when creating GetFilesToWord , as well as when creating a simple SearchToUser .

+2
source

Source: https://habr.com/ru/post/1308190/


All Articles