Reputation: 601
What is the particular use of a super keyword in android?
I've read that if you have an overridden method, and want to use the original method from a parent class, you call super. whatever.
But in the example that I'm following, the author of the tutorial has called super.setitle("blah blah"); within an oncreate method that has been overridden.
@Override
public void onCreate(Bundle savedInstanceState) {
super.setTitle("My Albums");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
I assumed super was to override something, just because it seems that way to me with the way the title is being added.?
Thanks for your help guys.
Upvotes: 2
Views: 7092
Reputation: 23
In override method if you want to call the child class’ functions then use “this” keyword, an other hand if you want to call parent class’ functions then you should use “super” keyword before the function's name with dot.
In android when you create an activity and you want to call the default behavior before your own logic or behavior then you use the “super” keyword.
Upvotes: 2
Reputation: 964
The keyword super in java allows you to call a super class's method implementation. This is used where a method is overridden in the current class but the intention is to call the method in super class.
e.g.
public class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public String getDescription() {
return "Person: " + name;
}
}
public class Employee extends Person {
public Employee(String name) {
super(name);
}
public String getDescription() {
return "Employee: " + name;
}
public String getPersonDescription() {
return super.getDescription();
}
}
public class Main {
public static void main(String args[]) {
Employee brian = new Employee("Brian");
System.out.println(brian.getDescription());
System.out.println(brian.getPersonDescription());
}
}
Upvotes: 0
Reputation: 6039
It is to execute the default functionality in the base class. After calling the base class you can add your own logic in the overridden version.
Upvotes: 4
Reputation: 20699
super
relates not to a specific method, but to the whole parent class instead. That's why your code sample is perfectly correct.
Upvotes: 0