You can invoke the constructors of the parent class from the base class. To know how to use of super keyword in java please read the constructor chaining first. It is used to invoke the constructor of the parent class. The JVM implicitly uses the super keyword from the constructor of the base class. Java super keyword can invoke the constructor of the parent classĪs we know, whenever we create an object of the class, the default constructor is automatically called by JVM. ("Class name : "+ super.className) Ĭlass name : MCA 2. To access the variable of ClassRecordwe will use the super keyword. When we are accessing the variable in the Record class, so compiler always prints the variable declared in the same class. ("Class name : "+ className) Īs you can see in the above example ClassRecord is the super class and Record is the sub class. To access the variables of the Superclass we need to use the super keyword. So, when we will access the variable in the Subclass the compiler automatically accesses the variables of the SubClass. SubClass can have variables exactly the same as in SuperClass. Suppose we are using inheritance in our classes. Java super keyword can access the variable of the parent class Java super keyword can access the method of the parent class 1. Java super keyword can invoke the constructor of the parent classģ. Java super keyword can access the variable of the parent classĢ. We use the super keyword to resolve these problems.ġ. But there may be a situation when we want to access the variables or method of the Superclass. So, when we access the variable or method in the Subclass the compiler always gives preference to the SubClass variables. Both classes contain the variable or method with the same name. Suppose you have two classes, SubClass, and SuperClass. We can refer to the variable, constructor, and method of a superclass by using the super keyword. It means the super keyword uses in subclasses to access the object of the superclass. So, we will recommend you please read the inheritance first, which will provide ease to understand the super keyword.Īs you know this keyword in java refers to the current object, in the same manner, the super keyword refers to the object of an immediate parent class. Let’s see how does super keyword in java with example and where we can use the super keyword.īasically, the super keyword in java comes in a role with the concept of inheritance. In java super keyword is used to refer to the object of the immediate parent class. In this post, we will discuss the super keyword and the use of super keyword in java. We already had a discussion on some keywords like the final keyword in java, static keyword in java, this keyword in java, etc.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |