Answer : C Answer : A Answer : C Answer : B Answer : A Answer : A Answer : B 28. Which statement transfer execution to different parts of your code based on the value of an expression?
Answer : C 29. Method Overriding is an example of
Answer : B 30. Which is a valid declaration of a String?
Answer : A Pages: 1 2 3 Read more articlesJava allows us to declare a variable whenever we need it. We can categorize all our variables into three categories, which have different-different scopes:
What Is Variable Shadowing?Variable shadowing happens when we define a variable in a closure scope with a variable name that is the same as one for a variable we've already defined in an outer scope. In other words, when a local variable has the same name as one of the instance variables, the local variable shadows the instance variable inside the method block. In the following example, there is an instance variable named
What Is variable Hiding?Variable hiding happens when we define a variable in a child class with the same name as one we defined in the parent class. A child class can declare a variable with the same name as an inherited variable from its parent class, thus hiding the inherited variable. In other words, when the child and parent classes both have a variable with the same name, the child class' variable hides the parent class' variable. In the following example, we are hiding the variable named
Variable Hiding Is Not the Same as Method OverridingWhile variable hiding looks like overriding a variable (similar to method overriding), it is not. Overriding is applicable only to methods while hiding is applicable to variables. In the case of method overriding, overridden methods completely replace the inherited methods, so when we try to access the method from a parent's reference by holding a child's object, the method from the child class gets called. You can read more about overriding on Everything About Method Overloading vs Method Overriding, Why We Should Follow Method Overriding Rules, and How Does the JVM Handle Method Overloading and Overriding Internally. But in variable hiding, the child class hides the inherited variables instead of replacing them, so when we try to access the variable from the parent's reference by holding the child's object, it will be accessed from the parent class. When an instance variable in a subclass has the same name as an instance variable in a super class, then the instance variable is chosen from the reference type.
In above example, when we call the overridden method
Because in the child class, the method is printing the child class' But when we call But if we wanted to access the child's variable even if we are using a parent reference, we can do that by using When our variables are private or in another package and have default access, such variables are not visible outside of that class, and the child class cannot access them. So there no confusion — and that is why we should always stick to General Guidelines to create POJOs and declare our variables with private access (and also provide proper get/set methods to access them). Do you want to know more about variable hiding?, In the article Why Instance Variable Of Super Class Is Not Overridden In SubClass, I have discussed why variables do not follow overriding, why variable hiding is not designed same as method overriding and why instance variable is chosen from reference type instead of the object? Please go ahead and read it. You can find the complete code on this GitHub Repository and please feel free to provide your valuable feedback. In which process a local variable has the same name as one of the instance?17) In which process, a local variable has the same name as one of the instance variables? Explanation: There are following reasons for considering a variable shadowing, they are listed below: When we define a variable in a local scope with a variable name same as the name of a variable defined in an instance scope.
Are local variables and instance variables the same?Local Variable: These variables are declared within a method but do not get any default value.
...
Difference between Instance Variable and Local Variable.. Can method and instance variable have same name?Yes it's fine, mainly because, syntactically , they're used differently.
When a local variable in an instance method has the same name as an instance field the instance field hides the local variable?A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field.
|