![]() ![]() However by using super keyword like this: thod_name you can call the method of parent class (the method which is overridden). For now you just need to remember this: When a child class overrides a method of parent class, then the call to the method from child class object always call the child class version of the method. We will learn method overriding in the next tutorials of this series. When a child class declares a same method which is already present in the parent class then this is called method overriding. 3) How to use super keyword in case of method overriding ![]() There are few important points to note in this example:ġ) super()(or parameterized super must be the first statement in constructor otherwise you will get the compilation error: “Constructor call must be the first statement in a constructor”Ģ) When we explicitly placed super in the constructor, the java compiler didn’t call the default no-arg constructor of parent class. Output: parameterized constructor of parent class * the compiler doesn't invoke the parent constructor automatically. * point to note is that when we explicitly use super in constructor * otherwise you will get a compilation error. * super() must be added to the first statement of constructor ("parameterized constructor of parent class") Let’s see an example to understand this: class Parentclass ![]() However when we have a constructor in parent class that takes arguments then we can use parameterized super, like super(100) to invoke parameterized constructor of parent class from the constructor of child class. It’s like explicitly doing something which would be implicitly done otherwise. We can call super() explicitly in the constructor of child class, but it would not make any sense because it would be redundant. Parameterized super() call to invoke parameterized constructor of parent class * invoke no-arg constructor of parent class automatically * it will invoke arg constructor of child class which will * Creating second object using arg constructor * will invoke child class constructor, which will * Creating object using default constructor. * The compiler still adds the no-arg super() here * Even though it is a parameterized constructor. * Compile implicitly adds super() here as the Let’s see an example to understand what I have explained above: class Parentclass It happens because compiler itself adds super()(this invokes the no-arg constructor of parent class) as the first statement in the constructor of child class. So the order to execution when we create the object of child class is: parent class constructor is executed first and then the child class constructor is executed. When we create the object of sub class, the new keyword invokes the constructor of child class, which implicitly invokes the constructor of parent class. 2) Use of super keyword to invoke constructor of parent class * this refers to the num variable of SuperclassĪs you can see by using super.num we accessed the num variable of parent class. * writing super.num in the print statement * Note that instead of writing num we are Let’s take the same example that we have seen above, this time in print statement we are passing super.num instead of num. ![]() * which is already present in the SuperclassĪccessing the num variable of parent class:īy calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have same variable. * The same variable num is declared in the Subclass Child class or subclass or derived class Parent class or Superclass or base class There is no way you can access the num variable of parent class without using super keyword. Lets take an example to understand this: In the following program, we have a data member num declared in the child class, the member with the same name is already present in the parent class. When you have a variable in child class which is already present in the parent class then in order to access the variable of parent class, you need to use the super keyword. Now lets discuss them in detail with the help of examples: 1) How to use super keyword to access the variables of parent class The use of super keywordġ) To access the data members of parent class when both parent and child class have member with same nameĢ) To explicitly call the no-arg and parameterized constructor of parent classģ) To access the method of parent class when child class has overridden that method. Before learning super keyword you must have the knowledge of inheritance in Java so that you can understand the examples given in this guide. The super keyword refers to the objects of immediate parent class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |