filmov
tv
Use of super Keyword in Java || Lesson 43 || Java Programming || Learning Monkey ||
![preview_player](https://i.ytimg.com/vi/IKZeq4O2Ib0/maxresdefault.jpg)
Показать описание
Use of super Keyword in Java
In this class, We discuss the Use of super Keyword in Java.
The reader should have prior knowledge of inheritance. Click Here.
Super Keyword:
Below are the uses of super keywords.
1) To call super class variables.
2) To call superclass methods.
3) To call superclass constructors.
We take examples and understand all three concepts.
The below diagram shows the code for calling super class variables.
In the above code method, “m2” uses a p-value.
c = p + p;
We have the p variable in both classes A and B.
The above line will look p value in class B.
The value of p is 50. so 50 + 50 =100.
Suppose we need to use the p-value from class A. We use super keywords.
c = super.p + p.
The super. p is the value from class A., i.e. 30.
The second p is the value from class B., i.e. 50.
Super is a keyword used to find the variables from the first occurrence of superclasses.
The below example shows the multi-level inheritance.
Super.p is checking in class B., but the p variable is not present in class B.
Check the p variable in class A. So get the p-value from class A.
The execution takes the first occurrence of the p-value in our superclasses.
Example of Methods:
The below diagram shows an example of calling superclass methods.
In the above code, all the classes have the m1 method.
If we need to use the m1 method from the superclass, we write super.m1().
Example of constructors:
The below code shows the example of calling the superclass constructor.
In the above code, we have a class weight.
The class “weight” constructor calls a superclass constructor using super().
Note: The first line in the constructor should be super.
Link for playlists:
In this class, We discuss the Use of super Keyword in Java.
The reader should have prior knowledge of inheritance. Click Here.
Super Keyword:
Below are the uses of super keywords.
1) To call super class variables.
2) To call superclass methods.
3) To call superclass constructors.
We take examples and understand all three concepts.
The below diagram shows the code for calling super class variables.
In the above code method, “m2” uses a p-value.
c = p + p;
We have the p variable in both classes A and B.
The above line will look p value in class B.
The value of p is 50. so 50 + 50 =100.
Suppose we need to use the p-value from class A. We use super keywords.
c = super.p + p.
The super. p is the value from class A., i.e. 30.
The second p is the value from class B., i.e. 50.
Super is a keyword used to find the variables from the first occurrence of superclasses.
The below example shows the multi-level inheritance.
Super.p is checking in class B., but the p variable is not present in class B.
Check the p variable in class A. So get the p-value from class A.
The execution takes the first occurrence of the p-value in our superclasses.
Example of Methods:
The below diagram shows an example of calling superclass methods.
In the above code, all the classes have the m1 method.
If we need to use the m1 method from the superclass, we write super.m1().
Example of constructors:
The below code shows the example of calling the superclass constructor.
In the above code, we have a class weight.
The class “weight” constructor calls a superclass constructor using super().
Note: The first line in the constructor should be super.
Link for playlists: