Java OOPC - Method Overriding & Super Keyword

 

Method Overriding





 

The process of changing the body of  method that inherit From superclass to subclass is called as the Method overriding.

Method overriding is a concept in object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass.



 



For overriding classes should be inherit. so because of that parent class properties and behaviours are claimed to child class. In here inside the sub class same method repeat two times. One is overrided sub class method and other is inherit method from parent class.

However, the overrided sub class method only runing and did not run the method inherit from parent class.

 

In a class first the the main method runing. Secondly  the Object is runing If we did not create any object JVM create a object from itself. Then Constructor runing and it is also if we did not create JVM itself created. Lastly normal methods are runing and the process inside that is go on.

 

 


Super key word

Super keyword use to call parent class constructor from subclass. Super keyword do call super class things.

 

The 'super' keyword in Java is a powerful tool when working with inheritance. It enables developers to navigate the complexities of parent-child class relationships, access members explicitly, and maintain a clear distinction between the properties of the parent and child classes.

 



Super( ); – parent class constructor

Super.methodname( ); – parent class specific mentioned method

Super.variablename; - parent class specify variable

 

From super keyword can access parent class constructor, parent class method and also variables.

 



 

The `super` keyword in Java provides several advantages when working with inheritance and object-oriented programming. Here are some key advantages of the `super` keyword:





 

Advantages of  super keyword


1. Access to Parent Class Members

2. Differentiating Between Parent and Child Class Members

3. Invoking Parent Class Constructors

4. Method Overriding and Invoking Parent Class Methods

5. Maintaining Code Readability

6. Avoiding Redundant Code

7. Flexibility in Code Modification

 

 `super` keyword in Java is a valuable tool for managing relationships between classes in an inheritance hierarchy. It promotes code reuse, enhances readability, and provides a clear mechanism for accessing and interacting with members of the immediate parent class.

 

Comments

Popular posts from this blog

Java OOPC -Encapsulation

Linked List Data Structures

Java OOPC -Class, Variables, Methods, Modifiers ,Main Methods