I'm wondering if there is some way to declare a variable that is member data of a base class so that it is not inherited by a class derived from it. I have some member data in my base class that should not be part of objects of the derived class, so I'd like to separate what should be inherited from what should not. Is there some way to do this?
If you're trying to 'hide' data from derived/inherited classes, use private access modifier.
Yes, even though they are inherited, you cannot access them unless they are marked protected or public.
Ric, yes. I think it's just not a feature available in C++. What I'd really like to do is create a base class with member data with some kind of prefix that prevents the data from being inherited by child classes. Something like: noinherit void func1(); or noinherit double x; Where noinherit is just some keyword I made up to define data that should not be inherited by child classes. In a way, I want to be able to determine the genes inherited by the children from the parent, instead of the children just getting the full set of the parent's genes, and simply having a certain phenotype based on which genes are private, and which are public or protected, to use a genetics analogy.
Related
I have a non abstract class called 'Player', and an abstract class called 'Score' with a subclass of 'Combination'. Within Combination there is and abstract method used in further subclasses.
How can I call my abstract method from the non-parent method 'Player' without making them static?
// This is the abstract method within Combination,
// it uses the face values from rolled dice to calculate the score.
abstract public int CalculateScore(int[] faceValues);
//Array of 'Score's in Player
private Score[] scores = new Score[10];
You cannot. This is access modifier and it was designed to narrow access to a class. To use it in your case, I suggest to change accessibility from abstract to public of a Score class.
If I understand you correctly, you want to implement CalculateScore in your derived class by accessing the scores field?
Simple answer is, you can't as you designed it that way. private modifier makes sure, only your own class can access the field. If you want derived classes to have access to that field, you have to change your modifier to protected on the scores-field.
If you do not want your derived classes to have access to scores directly, you have to implement methods with at least protected access, that modify the scores-field in a defined way, on the base class.
So your situation:
Player is a normal classs
Score is an abstract class
Combination is an abstract class too that subclasses Score. It contains an abstract method CalculateScore that you want to call from inside the Player class.
Player has an array of Scores
You can check if an element in your Score array is of type Combination then cast it.
if(score[0] is Combination)
{
(score[0] as Combination).CalculateScore(/* arguments here */);
}
You might need to rethink how your classes work though. IMHO, Player shouldn't be the one calculating the scores. Maybe use a ScoreCalculatorService to handle that.
Just trying to learn what is the best way to do it, I will try to explain my problem, it may seem a longer question, sorry for that,
I have a class Element, two classes Shape and Audio derives from it, further two classes Text and Image derive from Shape, and further class Video derive from Image. I need to implement a type property on each of these classes which must have a read-only nature as there is no sense of setting a type on any of these classes other than the actual type, for ex : a Type property on Image will always return a value Image and nothing else. So how do I implement this hierarchy which forces all classes to implement Type property? Each of these classes have their own functionalities other than base Element class which can be marked abstract, which will allow me to mark Type property as abstract which will force it to be implemented on derived class but only at one level, how do I enforce it be implemented on Video as well?
Please suggest me the best approach on this.
Thanks in Advance.
Create an interface which defines the Type property, make all classes that need to be forced to implement Type, inherit from this interface.
As you state, the base class can make a property abstract which will force it to be implemented by all derived classes. However, if your second tier of classes (Text and Image) are concrete, you cannot force derived classes (Video) to implement a method (since the concrete class must, by definition, be fully implemented).
You can easily mark the property as Virtual in the Text/Image classes so that it can be overridden by a derived class, but this won't force it.
Perhaps you could do something with generics, and a generic base class or interface, but that would depend on more knowledge of your design.
You may want to re-consider how you've designed the object hierarchy (if possible) - Perhaps Video should not derive from Image. Is it really an "is a" relationship? If not, perhaps you could either change the object hierarchy or use Interfaces rather than base classes to achieve the desired outcome.
I know the question sound somewhat stupid, but i have this scenario.
Context
I am creating 2 background tasks (METRO apps, winRT) which have much parts in common.
Now what i want is, a common base class defining common functionality, and 2 task to be derived from base class.
Problem: Background task should be targetted WinMD, and in this mode, it is not possible to create non sealed public class. If i create sealed, i cannot derive from it, and if i create private, i cannot expose the derived types because base is private.
Concern: What are the ways to achieve the desired functionality. (derived public types from private base). One way could be use of Composition (instead of inheritance)
You simply can't. .NET doesn't allow you to make a derived type more accessible than the base type. You'll have to expose the base type.
Note that if you only give it internal constructors, then other code won't be able to instantiate it or create their own derived types... what's the problem with exposing it at that point?
EDIT: Okay, I've just reread the question, and seen this:
it is not possible to create non sealed public class
So:
Could you make all the types involved internal?
Could you make it a public abstract class? (Does WinMD allow that?)
(If you have any other documentation around what's allowed in WinMD, that would be useful.)
If you're allowed to export interfaces, perhaps you could have your base class and derived classes internal, but make them implement public interfaces. Then have public static methods in a public static class to create instances of these internal types and return them via the interface. Again, I don't know if this is allowed - if you have any links to documentation around what's allowed in WinMD, that would be useful for those of us who haven't done any WinRT development.
Yes, you can hide it in the derived class, then expose the base.Property, i.e.,
new public ScrollBar HorizontalScrollBar
{
get { return base.HorizontalScrollBar; }
}
Since we can access the private data member of base class in the derived class with the help of friend function. How can we do the same in C# asp.net? I mean whats the alternative of friend function in C# asp.net?
internal is the access modifier for stuff that needs tobe accessible in a single assembly. protected is the modifier to get access to base class stuff.
If you have no control over the base class and need to 'hack' access, NHibernate does that sort of thing, e.g. when creating a performant setter on private instance fields. In this case the source code of the method NHibernate.Bytecode.Lightweight.ReflectionOptimizer.GenerateSetPropertyValuesMethod could be of interest to you.
You could use reflection... this might give you a start...
How do I use reflection to invoke a private method?
why "data members" should be declared "protected"?what can be the possible benifits?
Protected members are accessible by subclasses, which is not the case if you declare them private.
You declare things to be private or protected to hide them, so that only relevant things which are supposed to be used by the external world are exposed.
If you want to know why whould you want to hide members from the external world, you should get a book about object oriented probramming, because those are basic concepts.
protected simply means that subclasses are able to see that member, but the member is not part of the public API to the object.
Re the question; it depends what you mean by "data members". If you mean fields, then IMO they shouldn't be. It is fairly common to make some state (properties) protected, if subclasses would need that info (in particular methods), but it isn't necessary for the outside world.
A better example, however, is protected virtual, where the inheritor is able to change the implementation of an otherwise private (to the outside world) member. The classic example being:
protected virtual OnSomeMemberChanging(...) {}
protected virtual OnSomeMemberChanged(...) {}
where the inheritor can now react (or even block) changes to key values by using override to change the implementation (commonly but not always calling base.Whatever() at some point to invoke the original implementation as well).
From MSDN
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
Possible benefits: You restrict the access to the class and its immediate childrens