[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is new keyword needed to hide members?

Posted on 2009-02-14
16
Medium Priority
?
607 Views
Last Modified: 2012-06-27
Is the new keyword needed to hide base class members?  I can see no difference on the child member if new is used or not.  Only difference is that it removes the compiler warning.
0
Comment
Question by:brettr
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 23643856
The "New" keyword is used to set the variable equal to a new instance of an object. Normally object variables are initially set to NULL until they are explicitly given a value.

If you want to hide the members of a base class, use the "Private" or "Protected" keywords before the declaration. E.g.

Private MyObject oSomething;

Private means nobody can see it except the class itself. Protected means only the class and it's derived subclasses can see it, but a caller can't.
0
 
LVL 5

Accepted Solution

by:
Imperdonato earned 1000 total points
ID: 23643881
Yes and No.
Yes because a new modifier hides inherited members from a base class member,
            ex:
            public class baseClass {                  public void SomeMethod() {}            }            public class derivedClass : baseClass {                  new public void SomeMethod() {}            }
This member (SomeMethod) declared with the new modifier, will hide the same method from the base class.

No because a new operator creates objects and call constructors of the respective class.
            ex: classA objA = new classA();

Also read: http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx.

Hope that helps.

0
 
LVL 16

Assisted Solution

by:gnoon
gnoon earned 1000 total points
ID: 23643905
Yes. The 'new' modifier is mainly used for hiding the non virtual members, especially methods.
It does not replace the original like 'override'. Casts the instance to Base class to access the original version of member.

Visit here to get more helpful information
http://geekswithblogs.net/mohamed/articles/28613.aspx
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:brettr
ID: 23643918
Imperdonato and gnoon seem to get what I'm asking.  This isn't anything to do with creating objects.

@gnoon
Why do you say "Yes"?  I can leave off the new keyword and still hide members.  Please explain why you disagree.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23643954
A couple of points which might clear the picture somewhat:
                                      1. You can use new for non-virtual methods, events and properties(but you can not use override for these).
 2.
Take an example:

derivedClass derivedObj = new derivedClass();
 baseClass baseObj = derivedObj;
 baseObj.SomeMethod();
 -------------
 
 If the derivedObj.SomeMethod() is defined as "new", the above code will call the baseClass version. If it is defined as "override", it will call the derivedClass version.

3. If we have not specified any of these (new or override), the resulting output is similar to if we have used "new"; though the compiler generates a warning.

4. Using new you can change the return type of a method or property. Plus, it can help making a derived class more strongly typed than the base.

5. Consider this: A new version of a base class may be released which happens to introduce a method with the same name as a method in your derived class. Since these two methods have no relationship to eachother, except that the happen to have the same name, you certainly don't want to start overriding the new base method. new takes care of this.

Hope that helps.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 23643963
Sorry for confused word ;-) I mean yes because it just removes the warning of compiler. If 'new' is not specified, the member will behave as 'new' keyword were present.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23643977
And yes, you can leave off the new keyword, and hide members. I personally rarely use new as a modifier.
So No, we do not need the new keyword in most of the cases, unless we need to cater to some special scenario which only "new" can take care of.
0
 

Author Comment

by:brettr
ID: 23645564
@gnoon
Thanks for clarifying.

@Imperdonato
Your last post shows you are agreeing that new on derived methods/properties has no affect, which is true.   But the post just before it (ID: 23643954) says differently:

4. Using new you can change the return type of a method or property. Plus, it can help making a derived class more strongly typed than the base.

You're saying "new" allows you to change the return type of derived properties/methods.  This is not true.  Leave off "new" and you can still accomplish the same thing.   "new" also does nothing for making those members strongly typed.   Please prove me wrong.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23645914
@ImperdonatoYour last post shows you are agreeing that new on derived methods/properties has no affect, which is true.   But the post just before it (ID: 23643954) says differently:
No. Read my posts again. :)
                           
4. Using new you can change the return type of a method or property. Plus, it can help making a derived class more strongly typed than the base.
Yes. This means if you use the new modifier, the above thing is accomplished. However, if you've carefully read my post, (specifically Poine #3) then you would know that even if you leave the new modifier, Visual Studio compiler would take it as if you'd specified the keyword (other than a warning of course, and IMO, no warnings should be ignored, if ofcourse avoidable- which in this case is).

You're saying "new" allows you to change the return type of derived properties/methods.  This is not true.  Leave off "new" and you can still accomplish the same thing.   Yes. And I repeat "new" allows you to change the return type of derived properties/methods- and this is true. Just because compiler gives you the ease of not specifying the keyword, doesn't mean you should take the credit away from it :\

"new" also does nothing for making those members strongly typed.   Please prove me wrong.Okay. Consider this example:

public class baseClass{
     public void SomeMethod(){
         Console.WriteLine("This is base.");
     }
}

public class derivedClass: baseClass{
     new public void SomeMethod(){
         Console.WriteLine("This is the derived one");
     }
}

Now if we make calls like
baseClass baseObj = new baseClass();
derivedClass derivedObj = new derivedClass();
baseObj.SomeMethod();
derivedObj.SomeMethod();
The first call would call the base class' SomeMethod and would display "This is base.", and
the second call is calling SomeMethod from the derived class (i.e, "This is the derived one" would be displayed on the screen)

I hope we agree till this point.

Now, if you agree above, then that means the new modifier has made the member (the method in this case); strongly typed!
Read: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/2ea38b96-921c-4218-8c28-168f33b4bf62/)

Let me know if you still have doubts. :-)
0
 

Author Comment

by:brettr
ID: 23646307
And I repeat "new" allows you to change the return type of derived properties/methods- and this is true.
Still disagree with you on that.  You don't need new to change the return type.  "new" has zero affect but to be explicit to the compiler.
I hope we agree till this point... Now, if you agree above, then that means the new modifier has made the member (the method in this case); strongly typed!
No, we don't agree and the link you sent does nothing for your case here.  The method is void and returns nothing.  Even if it did return something, you can change the derived member's return type without "new".   Further, if you leave off new in your example, you'll notice the outcome is exactly the same.  "new" only tells the compiler we are being explicit that the derived method is unrelated to the base.  It does nothing for functionality or manipulates data in anyway.  Strongly type is up to the developer.  "new" doesn't do anything for  the member in regards to strongly typed.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23646952
Yup. But just to quote me from my last post: "Just because compiler gives you the ease of not specifying the keyword, doesn't mean you should take the credit away from it :\"
And I repeat again, just because compiler gives you an option not to specify the new modifier, doesnt mean new is not acting at the background. Hope you get it this time.
And btw, the example that I'd given earlier, does prove that with new you accomplish strongly typed members. SomeMethod becomes strongly typed, because no matter what, that is the method which will be called, unlike the example I stated in my post (ID: 23643954) Point # 5.
Just because you've the liberty not to specify the keyword, doesn't mean you are not using it. It's being used, though you might not know it.
(Similar to if you declare a class without specifying an access modifier, the compiler will take it as a private)


0
 

Author Comment

by:brettr
ID: 23647143
SomeMethod becomes strongly typed, because no matter what, that is the method which will be called
Again, it is void.  Where are you coming up with strongly typed?  It will be called regardless if new is there or not.  If you want a return type, fine.  C# is strongly typed and in the case of derived members, unless you are doing something dynamic, they will be strongly typed whether you want them to or not.   You mentioned of strongly typed in this case is irrelevant.
In regards to your point #5, you will only override if you use that keyword.  Otherwise, the child class member is used, "new' or not.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23647356
Again, it is void.  Where are you coming up with strongly typed?  
And why not?

It will be called regardless if new is there or not.  
Never denied that.

In regards to your point #5, you will only override if you use that keyword.  Otherwise, the child class member is used, "new' or not.
Consider this:

public class baseClass{
     public void SomeMethod(){
         Console.WriteLine("This is base.");
     }
}

public class derivedClass: baseClass{
     public void SomeMethod(){
         Console.WriteLine("This is the derived one");
     }
}

baseClass baseObj = new baseClass();
derivedClass derivedObj = new derivedClass();
baseObj.SomeMethod();
derivedObj.SomeMethod();

The output will of course be similar to the one we had seen earlier.
Now, suppose somebody makes a change to derivedClass' SomeMethod:
public class derivedClass: baseClass{
     public void SomeMethod(string id){
         Console.WriteLine("This is the derived one. Id: " + id.ToString());
     }
}

Now, if we make calls like
baseClass baseObj = new baseClass();
derivedClass derivedObj = new derivedClass();
baseObj.SomeMethod();
derivedObj.SomeMethod();


What do you think the output will be?

"This is base."
"This is base."

Now why this? Because you dint explicitly specified the new modifier, the compiler didn't give you an error earlier, after you changed derviedClass.SomeMethod.
So that lead to the base class method being called, though the callers' intention was to call SomeMethod of the derivedClass.
In large projects, this has a very high probability of occurrence.

I guess I have been clear enough this time, and that you will get it now.
Thanks.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23647364
Because you dint explicitly specified the new modifier, the compiler didn't give you an error earlier, after you changed derviedClass.SomeMethod.
^^Sorry, the compiler will give you a warning, not an error.
0
 

Author Comment

by:brettr
ID: 23647392
I'm not sure what you are wanting me to get.  

public void SomeMethod(string id){...}

is a completely different method and has nothing to do with strongly typed.  gnoon summed up the answer nicely and you were heading in a good direction as noted by my post ID: 23643918.  But now you've only demonstated how very confused you are.
0
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23647428
But now you've only demonstated how very confused you are.
Yeah right!
I don't really understand, where your confusion really is. My last post clearly gives an example to demonstrate the point I have been stressing.
Anyways, since this seems to be heading nowhere, plus the post has been closed, I won't be posting anymore. Later.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question