1

A question on a C# MTA exam I've done recently that has caused a large amount of discussion:

You have a class named Glass that inherits from a base class named Window. The Window class includes a protected method named break().

How should you call the Glass class implementation of the break() method?

A. Window.break();
B. Glass.break();
C. this.break();
D. base.break();

Can anyone give me a solid answer and rational for this?

  • that depends whether the `break()` method is overridden on `glass` class or not – Rahul Apr 26 '17 at 09:33
  • worth reading up about the protected keyword: https://msdn.microsoft.com/en-us/library/bcd5672a.aspx –  Apr 26 '17 at 09:33

3 Answers3

1

I would do this by simply calling Break();, As long as the Break() method is not declared as virtual (which makes it possible to override it). Calling using this or base is simply redundant.

However let´s say that Break() would be declared virtual then it would be the matter if you would want to call the implementation of Break() on the Window class (base.Break()) or on the Glass class (Break()/this.Break()).

Consider the following code

public class Window
{
    public virtual void Break()
    {
        Console.WriteLine("Break in window called");
    }
}

public class Glass : Window
{
    public override void Break()
    {
        Console.WriteLine("Break in Glass called");
    }

    public void DoSomething()
    {
        Break();
        this.Break(); // Same as above line
        base.Break();
    }
}

The output when calling DoSomething() on an instance of Glass would be

Break in Glass called
Break in Glass called
Break in window called
Simon Karlsson
  • 4,090
  • 22
  • 39
0

Question:

How should you call the Glass class implementation of the break() method?

Example

In this example they have an class named Glass. This class has a method called Break() that comes from the base class Window.

They want you to call the implemented method in the Glass class "call the Glass class implementation of the break() method"

To create an own version of a base class method you need to make it over writable. To do this add virtual to the base class method and in the derived class Glass add override to override that base class method.]

Next you can call different version of the method the derived method and base method. See Example for more details

This example will look like this:

 class Window
 {
     public virtual void Break()
     {
           // break method from the window class 
     }
 }

 class Glass : Window
 {
     public override void Break()
     {
           // This method comes from the base class Window. You want to override this one. They ask you to call this method.

           //To call the Break() mehod from Window:
           base.Break();
           // Call the Break() method from the current instance
           this.Break()
           Break(); 
     }
 }

Answer:

This answer is right because this one calls the current instance off the Glass class Break() method (See example)

C: this.break();

Other answers:

The Break() is not static because that would not make any sense and can't be made static in this question. They want a Glass to inherit from Window and want to call the Break() version of the Glass class. You need to override the Break() in the Glass class to create an own version of that method, Because of that you need to add virtual and override and virtual/override methods can not be made static. Because of that the first two answers are incorrect

A Window.Break()

This will call the static Break() Method from the Window class. (static is not used in this example this is not the answer)

B Glass.Break()

This will call the static Break() Method from the class. (static is not used in this example this is not the anwer)

C this.Break()

This will call the current instance Break() Method (See Example).

D base.Break()

This will call the Break() method from the current instance of base class Window.

Timon Post
  • 2,779
  • 1
  • 17
  • 32
  • To me the use of the form Glass and not glass implied a static method, not a mistyping of an object named glass, but perhaps they're not as pedantic as I'd hope on a software Development MTA – Orpheus Feal Apr 26 '17 at 09:54
  • The `Break()` is not static because that would not make any sense and can't be made static in this question. They want a `Glass` to inherit from Window and want to call the `Break()` version of the `Glass class`. You need to override the `Break()` in the `Glass class` to create an own version of that method, Because of that you need to add virtual and override and virtual/override methods can not be made static. – Timon Post Apr 26 '17 at 10:16
  • I'm certainly aware of that, but your first answer was "B: Glass .Break()". You've altered it since then, quickly, so it doesn't appear in the edits - my comment was referring to that. – Orpheus Feal Apr 26 '17 at 10:21
  • Aaaw I get it, It is right it was my first answer but I read the question again and I noticed that I was wrong and changed my answer to the right answer XD. – Timon Post Apr 26 '17 at 10:25
-1

B. Glass.break();

It is not specified in task from where it's called, so since there should be only 1 correct solution, only logical way would be to assume it can be called from anywhere. So if we implement break() as a public static method we'll be able to call it from anywhere and B would be the only logical and possible answer.

using System;
public class Window{
    protected void break1() {Console.Write("1");}
    public Window(){
        Glass.break1();
    }
}
public class Glass : Window{
    public static void break1() {Console.Write("2");}
    public Glass() {
        Glass.break1();
    }
}
public class Dijete : Glass{
    public Dijete() {
        Glass.break1();
    }
}
public class Program
{
    public static void Main()
    {
        Glass.break1();
    }
}

Please correct my logic if I'm wrong, but on exam dumps I also found B as correct answer.

PS: I called it break1, because calling it break() won't work, which actually makes this whole task make no sense.

vanjavk
  • 117
  • 1
  • 10