Asked by Louca Kaoulla on Jul 19, 2024

verifed

Verified

Consider the following class definitions.public class BClass
{
Private int x; public void set(int a) { x = a; } public void print() { }
}public class DClass extends BClass
{
Private int y; public void set(int a, int b)
{
//Postcondition: x = a; y = b;
}
Public void print() { }
}Which of the following is the correct definition of the method set of the class DClass?(i)
Public void set(int a, int b)
{
Super.set(a) ;
Y = b;
}(ii) public void set(int a, int b)
{
X = a;
Y = b;
}

A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) None of these

Method Set

The collection of methods that are available for use within a particular class or object.

Postcondition

A condition or predicate that must always be true immediately after the execution of a section of code or a function.

Super

A keyword used in object-oriented programming languages to refer to an object's parent class, often used to access parent class properties or methods.

  • Comprehend the idea and implementation of method overriding and overloading within object-oriented programming.
  • Comprehend the application and importance of the super and this keywords in accessing constructors and methods of a class and its superclass.
verifed

Verified Answer

SS
Stacy SpranoJul 24, 2024
Final Answer :
A
Explanation :
(i) is correct because it correctly uses `super.set(a)` to access the `set` method of the superclass `BClass` to set the value of `x`, and then sets `y` directly within `DClass`. (ii) is incorrect because it attempts to directly access `x`, which is private in the superclass and not directly accessible from `DClass`.