1

I'm checking out this tutorial: http://www.developerfeed.com/building-todo-list-app-android-using-sqlite

What I do not understand is this piece of code:

public Task()
{
    this.taskName=null;
    this.status=0;
}
public Task(String taskName, int status) {
    super();
    this.taskName = taskName;
    this.status = status;
}

Why the need to call super() on this point?

Edit: The reason why I'm asking is because I personaly see no need to put it there.

codeclog
  • 19
  • 3
  • 3
    Do you understand what `super();` does? – Dan S Jul 16 '14 at 23:03
  • possible duplicate of [unnecessary to put super() in constructor?](http://stackoverflow.com/questions/2054022/unnecessary-to-put-super-in-constructor) – clearlyspam23 Jul 16 '14 at 23:05
  • possible duplicate of [Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?](http://stackoverflow.com/questions/488727/inheritance-in-java-creating-an-object-of-the-subclass-invokes-also-the-constr) – Dan S Jul 16 '14 at 23:06
  • 1
    I understand what super() does. That's why I'm asking, because to me it seems unnecessary. But I might be missing something. – codeclog Jul 16 '14 at 23:21

5 Answers5

3

Why the need to call super() on this point?

There is no such need. The compiler will put it in by default.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

There is not a need to call super() with no arguments. When you call a constructor of a sub class the 0 parameter superclass constructor gets called automatically. It does not matter if you call it or not.

DarrenCibis
  • 865
  • 10
  • 25
1

super() specifically calls the constructor of the class that the current class is inheriting.

In this case, the call for the super() method is calling the constructor for the Object class, which all classes primitively inherit.

And you are right; in this particular case, the call for super() is not required or needed at all.

An example of using super() can be easily made with the reference of the JFrame class.

public class MyFrame extends JFrame {

public static final String TITLE = "My JFrame";

public MyFrame() {
    //call constructor of super class (JFrame)
    //the String parameter is the title of the frame
    super(TITLE);

    //other code here
}

//assume other methods are here
}
0

super(), is for calling the father constructor , i think that your class is not extending from some class, so in JAVA the pure class, extend from Object. When you call super(), you are calling the father constructor in this case Object(). I your constructor public Task(String taskName, int status) You are not calling to public Task() your class is calling the Object constructor Object().

Check this. http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Chefes
  • 1,892
  • 18
  • 17
0

Super is used to call the parent class. When you extend a class you can call a function or variable from the parent class in the child class.

For example Look at this pseudo code which child uses an int defined in the father class

Public class Child extends Father{
Int age = 21;
Int ageDiff = Super.age - age;
}

Super in your code is not needed.

NightSkyCode
  • 1,141
  • 2
  • 16
  • 33