23

What does the super method do?

public DataFetch(Context context) {
    super();
    this.ctx = context;
}

Does this constructor make the context of the newly created object the context of the super class? Not 100% sure how this works. So would the super() method essentially just be saying "put me into super class mode" to say it in lay man's terms?

1
  • 1
    NB it's not a 'method', it is a way of referring to a constructor in the base class.
    – user207421
    Commented May 25, 2011 at 5:19

5 Answers 5

37

It says "initialize my parent class before you initialize me" by calling its default constructor.

2
  • does that essentially mean that the context of the new object is the context of the super class in this case?
    – mergesort
    Commented May 25, 2011 at 1:04
  • 1
    No, the superclass has no direct references to any variables defined in your class. You would have to call a constructor that has a Context argument.
    – dmon
    Commented May 25, 2011 at 1:25
10

super() calls the parent's class constructor (all the way back to Object) and it runs before the current class's constructor.

2
  • Means parent class cannot be abstract class
    – Fennekin
    Commented Dec 29, 2022 at 17:44
  • @Fennekin An abstract class can have a constructor (which should not be public though). For instance you can define a package private/protected constructor for templating, or even a private constructor (not sure why but it's possible)? Commented Feb 27 at 10:30
2

There is no such thing as "superclass context" the context is an interface to access information from the application environment like image resources, system services ect. The context will be from whatever you pass in and is class independent. For instance, an Activity is the implementation of the Context interface you are likely to be using and any view you make from within that activity will have the same Context which is actually that provided by the activity.

0

When used in a constructor, the super() keyword appears alone and must be used before the this keyword can be used. The this keyword may then be used to call functions on a parent object.

0

The super() is essential when the parent class constructor takes any parameter

The super() is not necessary when the parent class constructor takes no parameter because super() will be already specified implicitly.

Look at the code:

public class A{  
    int a;
    A(int a){
        this.a=a;
        //Constructor of parent class which requires a parameter
    }   
}  
public class B extends A{
    int b;
    B(int a, int b){
        super(a);
        this.b=b;
        //here super(a) is used to pass the parameter required by the 
        constructor of the parent class A.
    }
}

In other words, whenever you create an object for the child class. First, the constructor of the parent class will be called followed by the constructor of the child class. So it is necessary to pass the parameters required by the parent class constructor from the child class constructor using super() method.

Hope it helps!

Please don't forget to upvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.