2

I was going through a piece of code in an android Malware to understand its working. av class was defined as below

public final class av implements android.widget.AdapterView.OnItemClickListener {
    final ChooserActivity a;
    public av(ChooserActivity chooseractivity) {
        super();
        a = chooseractivity;
    }
    ...Other methods here
}

super() invokes the immediate parent class constructor -> this I know

Parent class is Object. So the constructor will return what. Meaning what will be the properties of returned object.?

Nik theGeeK
  • 181
  • 12

8 Answers8

5

super() must be in the first statement inside the subclass constructor,it calls the parent constructor

public av(ChooserActivity chooseractivity)
{
    super();
    a = chooseractivity;

}
Nambi
  • 11,944
  • 3
  • 37
  • 49
3

It does nothing.

It is a call up to the parent constructor, the parent constructor of ac is Object.

Object is the default class your object extends from and it's constructor does nothing. (it actually doesn't have one)

Also you must call super() as the first line of your constructor, and by convention you should name your classes with an uppercase character.

public final class Av implements android.widget.AdapterView.OnItemClickListener {

    final ChooserActivity a;

    public Av(ChooserActivity chooseractivity) {
        super();
        this.a = chooseractivity;

    }
}

but because the class you are extending is Object you actually don't need to call super.

public final class Av implements android.widget.AdapterView.OnItemClickListener {

    final ChooserActivity a;

    public Av(ChooserActivity chooseractivity) {
        this.a = chooseractivity;

    }
}

You should probably read up on Understanding Java Constructors

Blundell
  • 75,855
  • 30
  • 208
  • 233
2

The code you have listed is invalid and will not compile. The call to super() must be the first statement in the constructor. It is used to construct the object you are extending before you go on and construct yourself.

Tim B
  • 40,716
  • 16
  • 83
  • 128
1

This code will not even compile. Remember constructor call must be the first statement in a constructor.

Try this:

public av(ChooserActivity chooseractivity) {
    // Call the base class constructor. Should be the firs
    super();
    a = chooseractivity;
}

OR

public av(ChooserActivity chooseractivity) {
   // Compiler will give implicit call to the base class contructor
    a = chooseractivity;
}
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
1

The super() keyword calls the parent constructor, i.e. the constructor of the class that av descends from. In this case, you haven't explicitly given a parent class so you end up with the default parent class of Object.

In this particular case, what super() will do there is break because it is illegal in Java to have a call to super() that is not the first line of the constructor method. That's because it doesn't make sense to construct the parent (more basic) object before the child (more specific) object.

More on super() on stackoverflow:

super() in Java When do I use super()?

Community
  • 1
  • 1
chm
  • 1,519
  • 13
  • 21
0

I guess you are getting an error for your code. The reason for this is that whenever you are calling a constructor of baseclass then super() must be the first line in method. Like below,

public final class av implements android.widget.AdapterView.OnItemClickListener
{

    final ChooserActivity a;

    public av(ChooserActivity chooseractivity)
    {
        super();                     // must be in first line.
        a = chooseractivity;
    }
}

However if you are calling method of the base class in that case super can be in any line.

public final class av implements android.widget.AdapterView.OnItemClickListener
{

    final ChooserActivity a;

    public void Test(ChooserActivity chooseractivity)
    {
        a = chooseractivity;
        super.Test();                     // No need to be declare in first line.
    }
}
user3291365
  • 445
  • 3
  • 14
0

Super must be the first statement as it is the proper constructor of the superclass to be called. There could be more than one, but the super() is called by default and so it is not necessary to call it explicitly. You could need to call super when you need to pass arguments.

holap
  • 438
  • 2
  • 18
0

The constructor call must be the first statement in a constructor. so super() should be at the beginning

Zied R.
  • 4,964
  • 2
  • 36
  • 67