super keyword for Method Overloading in Java
We use Method overloading to use a similar method for more than one time. This can be achieved by using different parameters in the signature. In the below example a class GFG with three similar methods is available, though the three methods are overloaded they are provided with different parameters. The object of the class GFG calls a method with a String Parameter.
Using Super Keyword:
If both parent & child classes have the same method, then the child class would override the method available in its parent class. By using the super keyword we can take advantage of both classes (child and parent) to achieve this.
- We create an object of child class as it can inherit the parent class methods.
- In the child class method, we call the method available in its parent class by using super().
In child class, a super keyword can be given on any line.
Single Int Parameter No parameter method
In the below example we have created an object of Programming which inherits two classes DP, CP. When the method learn is called using the object of Programming it goes to its own class Programming, in its class we have super.learn(); which calls the method super.learn() of its parent class and now after going to super.learn() in CP class it again calls super.learn() of its parent class i.e. from DP.
Dynamic Competitive Programming