Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A private member is not visible to child classes. I think protected virtual will perform the way you'd like?</p> <p><strong>UPDATE:</strong></p> <p>Here in greater detail is an explaination of what you can do with inheritance and overriding functions within C#. I tried to use a somewhat meaningful example, but consider it understood that its a poor class design and I wouldn't ever recommend implementing the classes described in this way. However, I hope perhaps this will give you an avenue to approach solving your original problem in a manner that might be acceptable. There is no way to prevent a concrete class from calling any of its members, but if your structure is like this in anyway, perhaps its not issue.</p> <pre><code>public abstract class Animal { public void DisplayAttributes() { Console.WriteLine(Header()); Console.WriteLine("Name: " + Name()); Console.WriteLine("Legs: " + Legs()); Console.WriteLine(); } protected virtual int Legs() { return 4; } private string Header() { return "Displaying Animal Attributes"; } protected abstract string Name(); } public class Bird : Animal { protected override string Name() { return "Bird"; } protected override int Legs() { return 2; } } public class Zebra : Animal { protected override string Name() { return "Zebra"; } } public class Fish : Animal { protected override string Name() { return "Fish"; } protected override int Legs() { return 0; } private string Header() { return "Displaying Fish Attributes"; } protected virtual int Gils() { return 2; } public new void DisplayAttributes() { Console.WriteLine(Header()); Console.WriteLine("Name: " + Name()); Console.WriteLine("Gils: " + Gils()); Console.WriteLine(); } } class Program { static void Main(string[] args) { Bird bird = new Bird(); bird.DisplayAttributes(); //Displaying Animal Attributes //Name: Bird //Legs: 2 Zebra zebra = new Zebra(); zebra.DisplayAttributes(); //Displaying Animal Attributes //Name: Zebra //Legs: 4 Fish fish = new Fish(); fish.DisplayAttributes(); //Displaying Fish Attributes //Name: Fish //Gils: 2 List&lt;Animal&gt; animalCollection = new List&lt;Animal&gt;(); animalCollection.Add(bird); animalCollection.Add(zebra); animalCollection.Add(fish); foreach (Animal animal in animalCollection) { animal.DisplayAttributes(); //Displaying Animal Attributes //Name: Bird //Legs: 2 //Displaying Animal Attributes //Name: Zebra //Legs: 4 //Displaying Animal Attributes //Name: Fish //Legs: 0 //*Note the difference here //Inheritted member cannot override the //base class functionality of a non-virtual member } } } </code></pre> <p>In this example, Bird, Zebra, and Fish could all call their Name and Legs methods, but within the context if this example, there wouldn't necessarily be utility in doing so. Additionally, as shown by Fish, the DisplayAttributes() can be modified for an instance of a concrete derived class; but when you're looking at an Animal, as in the foreach loop, you get the base classes DisplayAttributes behavior, regardless of the actual type of animal. I hope this may help povide the type of functionality you would like to replicate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload