1 / 7

super keyword in Java

https://firstcode.school/super-keyword-in-java/

Sudhanshi
Download Presentation

super keyword in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Super keyword in Java by Firstcode.school

  2. super keyword in Java The “super” keyword is an essential component of Java programming that enables the interaction between child and parent classes. It has a variety of applications in Java, making it essential to understand programmers. The “super” keyword is essential when working with inheritance and encapsulation in Java programming. Understanding its usage will enable programmers to write more efficient and effective code. In this article, we will discuss, Use of the ‘super’ keyword to call the parent class constructor Use of the ‘super’ keyword to call parent class methods Use of the ‘super’ keyword to access superclass variables Use of the ‘super’ keyword to access superclass variables and methods from nested classes

  3. Use of the ‘super’ keyword to call parent class constructor: The ‘super’ keyword in Java can be used to call the constructor of the parent class. When a subclass is created, the constructor of the parent class is automatically called before the constructor of the subclass. However, if the parent class has multiple constructors, it is possible to use the ‘super’ keyword to call a specific constructor. This is useful when the subclass needs to inherit some properties from the parent class, but also wants to add some additional functionality. By calling the parent class constructor with the ‘super’ keyword, the subclass can reuse the code of the parent class constructor, and then add its own code to the constructor.

  4. Use of the ‘super’ keyword to call parent class methods: When a subclass extends a parent class, it inherits all the methods of the parent class. Sometimes, a subclass may want to use the methods of the parent class but also add some extra functionality to them. In such cases, the ‘super’ keyword can be used to call the parent class methods. To call a parent class method using ‘super’, the method must be defined in the parent class and be accessible by the subclass. The ‘super’ keyword is used with the method name to call the parent class method.

  5. Use of the ‘super’ keyword to access superclass variables: In Java, it is possible to declare variables with the same name in both the superclass and subclass. This is known as hiding variables, and it can cause confusion when trying to access the variables from within the subclass. To avoid this, the ‘super’ keyword can be used to access the superclass variables. When a variable is declared with the same name in both the superclass and subclass, the subclass variable hides the superclass variable. This means that if the subclass wants to access the superclass variable, it must use the ‘super’ keyword. The ‘super’ keyword is used with the variable name to access the superclass variable.

  6. Use of the ‘super’ keyword to access superclass variables and methods from nested classes: Nested classes have access to the members of the outer class. However, if a nested class defines a member with the same name as a member of the outer class, the nested class member hides the outer class member. In such cases, the ‘super’ keyword can be used to access the hidden members of the outer class. Similarly, the ‘super’ keyword can also be used to access the members of the superclass from the nested class. This is useful when a subclass has overridden a method or hidden a variable from its superclass, but still wants to access the superclass member.

  7. Conclusion In conclusion, the ‘super’ keyword in Java is an important concept for Java programmers to understand. It allows for efficient reuse of code and access to superclass variables and methods in a subclass. The ‘super’ keyword can be used to call the constructor of the parent class, call methods of the parent class, access hidden variables from the superclass, and access superclass variables and methods from nested classes. By understanding the use of the ‘super’ keyword, Java programmers can write more efficient and concise code, making their programs easier to maintain and develop.

More Related