Junior — Middle
Will the object change if its method is overridden in a subclass?
sobes.tech AI
Answer from AI
If a method of the base class is overridden in a subclass, then calling this method on an object of the subclass will use the new implementation. The object itself as an instance of the subclass does not change, but the behavior of the method does. This allows modifying or extending functionality without changing the original class.
Example in C#:
class BaseClass
{
public virtual void ShowMessage()
{
Console.WriteLine("Message from the base class");
}
}
class DerivedClass : BaseClass
{
public override void ShowMessage()
{
Console.WriteLine("Message from the subclass");
}
}
// Usage
BaseClass obj = new DerivedClass();
obj.ShowMessage(); // Outputs: "Message from the subclass"
Thus, the object does not change as a structure, but its behavior via the method is changed.