C# Inheritance Discussion from MTA

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?

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

Inheritance, Inheritance in C# enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. C# - Inheritance - One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes

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.

[PDF] mta, author of the Windows Development Exam Review Kit in the MTA. Exam Review In C#, a colon is used to indicate inheritance, as in this example: public class� Inheritance (C# Programming Guide) 02/07/2020; 7 minutes to read +7; In this article. Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes.

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.

c#, 我最近完成的有关C MTA考试的问题引起了很多讨论: 您有一个名为Glass的类, 该类继承自名为Window的基类。 Window C# Inheritance Discussion from MTA. Inheritance, in C#, is the ability to create a class that inherits attributes and behaviors from an existing class. The newly created class is the derived (or child) class and the existing class is the base (or parent) class. Inheritance is one of the key features of object-oriented programming. The benefits of inheritance are part of the

[PDF] MTA 98 361 Study Guide, author of the Windows Development Exam Review Kit in the MTA. Exam Review In C#, a colon is used to indicate inheritance, as in this example: public class� C# Inheritance. In C#, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in other class.

NUnit.Framework, Normally, multiple SetUp methods are only defined at different levels of an inheritance hierarchy, as explained below. If a SetUp method fails or throws an� The purpose of inheritance is a bit more complex because inheritance serves two purposes, semantics and mechanics. Inheritance Semantics Inheritance captures semantics (meaning) in a classification hierarchy (a taxonomy), arranging concepts from generalized to specialized, grouping related concepts in subtrees, and so on.

MTA 98-361 Q&A Flashcards, You should consider making use of the switch C# statement. to make use of the inheritance object oriented programming concept. You are discussing the Inheritance is a hierarchical relationship between classes. Inheritance can denote a "is - a" relationship between classes. Inheritance is sometimes said to provide "weak encapsulation," because if you have code that directly uses a derived class then that code can be broken by changes to a base class.

Comments
  • that depends whether the break() method is overridden on glass class or not
  • worth reading up about the protected keyword: msdn.microsoft.com/en-us/library/bcd5672a.aspx
  • This was my thinking too, but others weren't so sure (Including the lecturer, it seems).
  • 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
  • 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.
  • 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.
  • 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.