You may want to reconsider actually inheiriting from your BaseClass if it has properties that you don't want to use or have access to.
You cannot hide Properties or Methods that are inheirited from a base class as this is constitutes a violation of one of the major concepts of Object Oriented Programming, the Liskov Substitution.
"Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it."
3.3 Declarations (extract from "C#, Language Specification", Version 4.0)
Note that base classes do not contribute to the declaration space of a class, and base interfaces do not contribute to the declaration space of an interface.
Thus, a derived class or interface is allowed to declare a member
with the same name as an inherited member.
Such a member is said to hide the inherited member.
Example:
void Main()
{
Pawar pawar = new Pawar();
Console.WriteLine (pawar.PawarName);
Gerry gerry = new Gerry();
Console.WriteLine (gerry.PawarName);
}
public class Pawar
{
public System.String PawarName { get; set; }
public Pawar() // constructor
{
PawarName = "Pawar";
}
}
public class Gerry : Pawar
{
public System.String PawarName { get; set; } // hides Pawar.PawarName
public Gerry()
{
PawarName = "Gerry";
}
}
output:
Pawar
Gerry
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
We override methods, as well as properties; the c# specification is clear on this, for example:
10.6.4 Override methods (extract from "C#, Language Specification", Version 4.0)
Only by including an overridemodifier can a method
override another method.
In all other cases, a method with the same signature as an inherited method simply
hides the inherited method.
In the example
class A
{
public virtual void F() {}
}
class B: A
{
public virtual void F() {} // Warning, hiding inherited F()
}
the F method in B does not include an override modifier and therefore does not override the F method in A. Rather, the F method in Bhides the method in A, and a warning is reported because the declaration does not include a new modifier.
In the example
class A
{
public virtual void F() {}
}
class B: A
{
new private void F() {} // Hides A.F within body of B
}
class C: B
{
public override void F() {} // Ok, overrides A.F
}
the F method in B hides the virtualF method inherited from A. Since the new F in B has private access, its scope only includes the class body of B and does not extend to C. Therefore, the declaration of F in C is permitted to override the F inherited from A.
10.7 Properties (extract from "C#, Language Specification", Version 4.0)
For a property or indexer that includes an override modifer, an accessor must match the accessor-modifier, if any, of the accessor being overridden.
in the O.P., look at this:
public class BaseClass
{
public string property1 { get; set; }
public string property2 { get; set; }
public string property3 { get; set; } // public
}
public class DerivedClass : BaseClass
{
private override string property3 { get; set; } // private here is illegal (10.7.2 Accessors)
}
in
pawar.vikaskwd's example above, in the two classes, the accessors
do not match. The first is public, the second is private.
Rion, you will not find the word "shadow" in the c# language specification:
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a
class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I understand that and although the specification refers to this as "hiding" the property, however the new property in the derived class is basically just overriding it, as the property will still be accessible in the new derived class.
void Main()
{
Pawar pawar = new Pawar();
Gerry gerry = new Gerry();
Console.WriteLine (" gerry.PawarName: {0}", gerry.PawarName); // hidden
Console.WriteLine ("pawar.PawarName: {0}", pawar.PawarName); // not hidden
}
public class Pawar
{
public System.String PawarName { get; set; }
public Pawar() { PawarName = "Pawar"; } // constructor
}
public class Gerry : Pawar
{
public System.String PawarName { get; set; } // hides Pawar.PawarName
public Gerry() { PawarName = "Gerry"; } // constructor
}
in the example above, the overridemodifier would not be legal as discussed earlier in this thread.
output:
gerry.PawarName: Gerry
pawar.PawarName: Pawar
compare with this:
void Main()
{
Pawar pawar = new Pawar();
Gerry gerry = new Gerry();
Console.WriteLine (" gerry.PawarName: {0}", gerry.PawarName); // hidden
Console.WriteLine ("pawar.PawarName: {0}", pawar.PawarName); // not hidden
}
public class Pawar
{
public virtual System.String PawarName { get; set; } // virtual
public Pawar() { PawarName = "Pawar"; } // constructor rName; }
}
public class Gerry : Pawar
{
public override System.String PawarName { get; set; } // hides Pawar.PawarName
public Gerry() { PawarName = "Gerry"; } // constructor
}
with similar output:
gerry.PawarName: Gerry
pawar.PawarName: Pawar
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I understand the two are different, I was just saying that although the term "hiding" is mention the specification, the actual property is still accessible, which may be different that the intent of the OP.
I believe that the intention of the OP was to actually have the same property not accessible at all from the derived class, which is why he included private in his initial question.
(Not trying to question your fine explanation on the topic, just stating what I thought OP's intention was.)
N.B.: it is important to know that accessibility blocks
down, not up.
The following code compiles and runs error free:
void Main()
{
MyBase myBase = new MyBase();
Console.WriteLine("Base: {0}", myBase.BaseString);
DerivedFromMyBase derivedFromMyBase = new DerivedFromMyBase();
Console.WriteLine(" Down One: {0}", derivedFromMyBase.BaseString);
Console.WriteLine(" Down One: {0}", derivedFromMyBase.BelowBase);
DerivedFromDerived derivedFromDerived = new DerivedFromDerived();
Console.WriteLine(" Down Two: {0}", derivedFromDerived.BaseString);
Console.WriteLine(" Down Two: {0}", derivedFromDerived.BelowBase);
Console.WriteLine(" Down Two: {0}", derivedFromDerived.BelowDerived);
}
public class MyBase // Top Level, i.e., "Base"
{
public System.String BaseString = "base"; // can be used in derived classes}
}
public class DerivedFromMyBase : MyBase // Down One Level from Top Level
{
public System.String BelowBase { get; set; }
public DerivedFromMyBase() // constructor
{ BelowBase = BaseString; }
}
public class DerivedFromDerived : DerivedFromMyBase // Down Two Levels from Top Level
{
public System.String BelowDerived { get; set; }
public DerivedFromDerived() // constructor
{ BelowDerived = BaseString; }
}
output:
Base: base
Down One: base
Down One: base
Down Two: base
Down Two: base
Down Two: base
However, this simple change blocks all levels below MyBase:
private System.String BaseString = "base"; // can NOT be used in derived classes}
By changing the accessibility of BaseString from public to
private, we prevent any derived class from using it.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
pawar.vikask...
Member
108 Points
57 Posts
Hide base class property in derived class
Feb 01, 2013 06:09 AM|LINK
This is my Base class
public class BaseClass { public string property1 { get; set; } public string property2 { get; set; } public string property3 { get; set; } }and this is my derived class
public class DerivedClass : BaseClass { private override string property3 { get; set; } }I want to hide proerty3 in derived class. but i am getting this error "virtual or abstract members cannot be private".
How i can hide the base class property?
Thanks in advance.
Rion William...
All-Star
27284 Points
4519 Posts
Re: Hide base class property in derived class
Feb 01, 2013 12:30 PM|LINK
You may want to reconsider actually inheiriting from your BaseClass if it has properties that you don't want to use or have access to.
You cannot hide Properties or Methods that are inheirited from a base class as this is constitutes a violation of one of the major concepts of Object Oriented Programming, the Liskov Substitution.
"Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it."
More on Liskov Subtitution can be found in this Stack Overflow discussion.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hide base class property in derived class
Feb 01, 2013 05:32 PM|LINK
@ pawar.vikaskwd
c# provides this mechansim for you.
3.3 Declarations (extract from "C#, Language Specification", Version 4.0)
Note that base classes do not contribute to the declaration space of a class, and
base interfaces do not contribute to the declaration space of an interface.
Thus, a derived class or interface is allowed to declare a member with the same name as an inherited member.
Such a member is said to hide the inherited member.
Example:
void Main() { Pawar pawar = new Pawar(); Console.WriteLine (pawar.PawarName); Gerry gerry = new Gerry(); Console.WriteLine (gerry.PawarName); } public class Pawar { public System.String PawarName { get; set; } public Pawar() // constructor { PawarName = "Pawar"; } } public class Gerry : Pawar { public System.String PawarName { get; set; } // hides Pawar.PawarName public Gerry() { PawarName = "Gerry"; } }output:
g.
Rion William...
All-Star
27284 Points
4519 Posts
Re: Hide base class property in derived class
Feb 01, 2013 05:47 PM|LINK
@gerrylowry
Isn't this typically referred to as "shadowing" the property or it's basically just overriding it?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hide base class property in derived class
Feb 02, 2013 03:39 AM|LINK
@ Rion Williams
We override methods, as well as properties; the c# specification is clear on this, for example:
10.6.4 Override methods (extract from "C#, Language Specification", Version 4.0)
Only by including an override modifier can a method override another method.
In all other cases, a method with the same signature as an inherited method simply hides the inherited method.
In the example
class A { public virtual void F() {} } class B: A { public virtual void F() {} // Warning, hiding inherited F() }the F method in B does not include an override modifier and therefore does not override the F method in A. Rather, the F method in B hides the method in A, and a warning is reported because the declaration does not include a new modifier.
In the example
class A { public virtual void F() {} } class B: A { new private void F() {} // Hides A.F within body of B } class C: B { public override void F() {} // Ok, overrides A.F }the F method in B hides the virtual F method inherited from A.
Since the new F in B has private access, its scope only includes the class body of B and does not extend to C.
Therefore, the declaration of F in C is permitted to override the F inherited from A.
10.7 Properties (extract from "C#, Language Specification", Version 4.0)
For a property or indexer that includes an override modifer,
an accessor must match the accessor-modifier, if any, of the accessor being overridden.
in the O.P., look at this:
public class BaseClass { public string property1 { get; set; } public string property2 { get; set; } public string property3 { get; set; } // public }public class DerivedClass : BaseClass { private override string property3 { get; set; } // private here is illegal (10.7.2 Accessors) }in pawar.vikaskwd's example above, in the two classes, the accessors do not match. The first is public, the second is private.
Rion, you will not find the word "shadow" in the c# language specification:
c#: http://msdn.microsoft.com/en-us/library/vstudio/435f1dw2.aspx
"new Modifier (C# Reference)"
vb.NET http://msdn.microsoft.com/en-us/library/1h3wytf6.aspx
"Shadows (Visual Basic)"
g.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hide base class property in derived class
Feb 02, 2013 11:34 AM|LINK
@ pawar.vikaskwd
please check MSDN regarding override
to hide the base class property, please see my replies, above.
N.B.: http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx "override (C# Reference)"
The override modifier is required to extend or modify the
abstract or virtual implementation of an inherited method, property, indexer, or event.
pawar.vikaskwd, in your example, your base class not "abstract or virtual".
http://msdn.microsoft.com/en-us/library/sf985hc5.aspx
abstract (C# Reference)
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx
virtual (C# Reference)
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
g.
Rion William...
All-Star
27284 Points
4519 Posts
Re: Hide base class property in derived class
Feb 02, 2013 01:14 PM|LINK
@gerrylowry
I understand that and although the specification refers to this as "hiding" the property, however the new property in the derived class is basically just overriding it, as the property will still be accessible in the new derived class.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hide base class property in derived class
Feb 02, 2013 06:04 PM|LINK
@ Rion Williams
hiding and overriding are different.
please see:
Eric Lippert's answer to:
http://stackoverflow.com/questions/1508350/why-does-this-polymorphic-c-sharp-code-print-what-it-does/1510702#1510702
(additionallly, note Jon Skeet's comment to the reply by Blindy).
and also
http://stackoverflow.com/questions/11046387/exact-difference-between-overriding-and-hiding.
Eric Lippert was a member of Microsoft's language design committee.
http://social.msdn.microsoft.com/profile/Eric%20Lippert
http://programmers.stackexchange.com/users/6505/eric-lippert
void Main() { Pawar pawar = new Pawar(); Gerry gerry = new Gerry(); Console.WriteLine (" gerry.PawarName: {0}", gerry.PawarName); // hidden Console.WriteLine ("pawar.PawarName: {0}", pawar.PawarName); // not hidden } public class Pawar { public System.String PawarName { get; set; } public Pawar() { PawarName = "Pawar"; } // constructor } public class Gerry : Pawar { public System.String PawarName { get; set; } // hides Pawar.PawarName public Gerry() { PawarName = "Gerry"; } // constructor }in the example above, the override modifier would not be legal as discussed earlier in this thread.
output:
compare with this:
void Main() { Pawar pawar = new Pawar(); Gerry gerry = new Gerry(); Console.WriteLine (" gerry.PawarName: {0}", gerry.PawarName); // hidden Console.WriteLine ("pawar.PawarName: {0}", pawar.PawarName); // not hidden } public class Pawar { public virtual System.String PawarName { get; set; } // virtual public Pawar() { PawarName = "Pawar"; } // constructor rName; } } public class Gerry : Pawar { public override System.String PawarName { get; set; } // hides Pawar.PawarName public Gerry() { PawarName = "Gerry"; } // constructor }with similar output:
g.
Rion William...
All-Star
27284 Points
4519 Posts
Re: Hide base class property in derived class
Feb 02, 2013 08:07 PM|LINK
@gerrylowry
I understand the two are different, I was just saying that although the term "hiding" is mention the specification, the actual property is still accessible, which may be different that the intent of the OP.
I believe that the intention of the OP was to actually have the same property not accessible at all from the derived class, which is why he included private in his initial question.
(Not trying to question your fine explanation on the topic, just stating what I thought OP's intention was.)
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Hide base class property in derived class
Feb 03, 2013 06:17 AM|LINK
@ Rion Williams, pawar.vikaskwd
FWIW, Rion, if your assumption about Pawar's intention is correct, then Pawar does not understand accessors.
http://msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.110).aspx "Accessibility Levels (C# Reference)"
http://msdn.microsoft.com/en-us/library/zd74a9ww.aspx "Accessibility Domain (C# Reference)"
http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx "Restrictions on Using Accessibility Levels (C# Reference)"
N.B.: it is important to know that accessibility blocks down, not up.
The following code compiles and runs error free:
void Main() { MyBase myBase = new MyBase(); Console.WriteLine("Base: {0}", myBase.BaseString); DerivedFromMyBase derivedFromMyBase = new DerivedFromMyBase(); Console.WriteLine(" Down One: {0}", derivedFromMyBase.BaseString); Console.WriteLine(" Down One: {0}", derivedFromMyBase.BelowBase); DerivedFromDerived derivedFromDerived = new DerivedFromDerived(); Console.WriteLine(" Down Two: {0}", derivedFromDerived.BaseString); Console.WriteLine(" Down Two: {0}", derivedFromDerived.BelowBase); Console.WriteLine(" Down Two: {0}", derivedFromDerived.BelowDerived); }public class MyBase // Top Level, i.e., "Base" { public System.String BaseString = "base"; // can be used in derived classes} }public class DerivedFromMyBase : MyBase // Down One Level from Top Level { public System.String BelowBase { get; set; } public DerivedFromMyBase() // constructor { BelowBase = BaseString; } }public class DerivedFromDerived : DerivedFromMyBase // Down Two Levels from Top Level { public System.String BelowDerived { get; set; } public DerivedFromDerived() // constructor { BelowDerived = BaseString; } }output:
Base: base Down One: base Down One: base Down Two: base Down Two: base Down Two: baseHowever, this simple change blocks all levels below MyBase:
By changing the accessibility of BaseString from public to private, we prevent any derived class from using it.
g.