3

default constructor invoke no_argument constructor in super class reference and this last constructor used to instantiate object from this class,How ? i mean what does superclass constructor body exactly do ?

example

public class Child extend Fragment {}

if i tried to instantiate this class Child

Child a = new Child() ;

while compiling compiler invoke no-argument constructor in Fragment class . in android.app package i found that no-argument constructor has no body

public Fragment() {}

Question how could this constructor be useful ? it do nothing !

P.S i mean by

how could this constructor be useful?

how does it instantiate its class although it empty , where is the code that instantiate super class?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Error
  • 820
  • 1
  • 11
  • 34

3 Answers3

2

Question how could this constructor be useful ? it do nothing !

The purpose of a constructor, whether empty or not, is to provide an instance of the class. The constructor body, if it exists, may further do additional operations necessary when first creating the instance.

The constructor may not be doing anything itself but it returns an instance, on which you may further call public methods defined in the class.

For example, in your mentioned Fragment class, let's suppose there is an attach() method. If the attach() method is non-static, you will need to create an instance (using the empty constructor) to call the attach() method on it irrespective of whether the constructor does anything or not.

how does it instantiate its class although it empty

A constructor doesn't need any code in its body to be instantiated. Calling new ClassName() (with appropriate parameters, if any) is enough. It is the job of JVM to create the object at runtime. You don't need to write any specific code inside a constructor for that. That extra code, if you wish, can help the instance start off with a specific state by setting some fields or calling methods, etc.

where is the code that instantiate super class?

A constructor in a subclass (or any class, for that matter) only generates an instance of that class. It doesn't need to instantiate its super class. You may call the super class constructor (using super()) if it has some important initialization code, but it's not compulsory. Also, subclasses don't inherit constructors from super classes. But they do inherit the public and protected fields and methods.

Regarding the Fragment class, you have have some confusion regarding its use which can be clarified here.

Community
  • 1
  • 1
bitbybit
  • 162
  • 1
  • 7
  • you misunderstood my question i am not asking if it do additional work beside instantiating object . i'am wondering how it instantiate object if it empty constructor – Error Oct 23 '15 at 13:24
  • 1
    @Error You don't need _any code_ inside constructor just to be able to instantiate the class. Please see edited answer. – bitbybit Oct 23 '15 at 14:04
1

The syntax for calling a superclass constructor is

super();  

or:

super(parameter list);

Question how could this constructor be useful ? it do nothing !

The super constructor could be used to initialized properties common for all subclasses. If it's empty it still can be used by reflection API.

Fragment.class.getConstructor().newInstance();

or

Child a = new Child() ;
a.getClass().getSuperclass().getConstructor().newInstance();

the last line explicitly invokes a constructor of the superclass.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

an empty constructor (aka default constructor) means only that an instance can be created without any further work. This can be due to framework requirements or the instantation is ok with the default values for the attributes.

If the empty constructor is the only constructor in the class, it is not required to write it, its there by default.

Although if there are other constructors (with parameters), one must explicitly state the default constructor.

Emerson Cod
  • 1,990
  • 3
  • 21
  • 39
  • i am not asking about meaning i am asking how ? This can be due to framework requirements this might be the answer , but need illustration – Error Oct 23 '15 at 12:52
  • not sure what exactly your question is... "how could this constructor be useful ? it do nothing !" - it does instantiate an instance as every constructor does ?! – Emerson Cod Oct 23 '15 at 12:56
  • yes , i know it does . i am asking ,How ? in Roman C answer he says it used in API reflection – Error Oct 23 '15 at 12:59
  • i'm not familiar with the framework you are using, so it might be reflections, but maybe somewhere is simply calling `new Fragment()` – Emerson Cod Oct 23 '15 at 13:01
  • "empty constructor (aka default constructor) " is not true as not all empty constructors are necessarily default constructors. The latter is created by the compiler so it's never coded explicitly. If an empty constructor is coded explicitly it's called an "no-argument constructor". "It’s not really a default constructor once it’s explicitly specified", see https://www.javacodegeeks.com/2020/08/jdk16-javac-xlint-warning-about-default-constructors.html. – dr0i Oct 25 '21 at 13:34