17F. Java Basics for Selenium - Super Keyword - Interview Questions
Автор: subbus tech
Загружено: 2019-03-17
Просмотров: 47
In this video we will see some interview questions about super keyword.
What is the super keyword?
Super keyword is used to invoke the overridden method which overrides one of its superclass methods. This keyword allows to access overridden methods and also to access hidden members of the superclass.
It also forwards a call from a constructor to a constructor in the superclass.
Why can’t we use super or this keyword in main method?
Static keyword is mainly used in java for efficient memory management. Static keyword can be applied to variables, methods, blocks and nested classes.
Static keyword belongs to the class rather than the instance of the class. Whenever a method is declared as static, it can only be accessed by its Class name. Static method can only have access to its static data members and methods. It cannot access non - static members i.e. instance variables.
Super keyword: It has the following uses,
• It’s a keyword which is a reference variable used to refer to the parent class object.
• It is used to invoke parent class methods.
• super() is used to invoke parent class constructor.
• super keyword is used to access the parent class’s instance variables.
For the above reasons, super keyword cannot be used under static method, it can only be used in non-static context.
This keyword: It has the following uses,
• It can be used to refer to the current class instance variable.
• It is used to refer to the current class object, (the object whose constructor or method is getting called). You can refer to any member of the current object from within an instance method or constructor using this.
Now since, static methods are related to the class and not to the objects, this cannot be used inside static methods.
Write some points about super keyword in java?
These are some uses of super keyword in java.
Super keyword can be used to refer parent class instance variables.
Super keyword can be used to invoke parent class method.
Super keyword can be used to invoke parent class constructor.
Can we access parent class variables in child class by using super keyword?
Yes, we can access parent class variable in child class by using java super keyword.
Can we call parent class method in sub class by using super keyword?
Yes, we can access parent class method in child class by using super keyword.
Can we call parent class constructor in sub class constructor by using super keyword?
Yes, we can call parent class constructor in sub class constructor by using super keyword but super keyword must be the first statement in sub class constructor.
Can we use both "this" and "super" in constructor?
No, It is not possible in java we cannot use both this and super keyword together in java constructor because this and super should be the first statement in any java constructor.
What is difference between this() and super()?
There are some difference between java this() constructor and super() constructor.
Java this()
It is used to access current class constructor.
It can be used inside another constructor of same class.
It can be used to remove ambiguity error when we have data members and local are same name.
For example: Remove ambiguity error
Java super()
It is used to access parent class constructor from sub class.
It can be used to only inside child class constructor.
It doesn't remove any ambiguity error from programs.
Can we use super keyword in static method of a sub class for calling parent class method?
No, we cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: