|
-
May 22nd, 2008, 04:10 PM
#1
About abstract/sealed/virtual/override/new keywords...
Hi all. I have following simple questions:
<1> What is the difference between abstract class and an interface?
I know the following-
- Fields/member vaiables are allowed in abstract class, where as they are not allowed in an interface.
- The class implementing an interface has to implement all the methods/properties/delegates in the interface, but a class inheriting an abstract class need not implement all the methods/properties/delegates in the base class which is abstract class. But I am not sure if the user has to simply ignore the methods or he has to declare them as abstract in the derived class if he is not going to implement that method?
- No constructors, destructors, garbage collection in an interface. Please comment on this one...
<2> In an enterprise development, which one comes first - an abstract class or an interface? This was an interview question.
<3> What is happens when the keyword abstract is used to declare and implement a method? What happens when a keyword sealed is applied to a method?
<4> Can we have virtual/override/new keywords attached to a contructor in C#? If yes, then answer the following:
Code:
class A
{
public virtual A(){ }
}
class B : A
{
public override B(){ }
}
class C : B
{
--------------- C(){ } // Mention the keyword(s) that will come here
// if the base class c'tor is to be invoked and otherwise
}
<5> What is the analogous of C# static keyword in VB.NET?
<6> Is there a C# equivalent for keywords My, AddressOf, With in C#? If not then how are the tasks done by them in VB.NET performed in C#?
<7> Which of the ways is cost effective? Boxing or Unboxing?
Although I have a rough idea of all the Qs, I am not 100% confident about them. So please give all your inputs...
Thankyou,
Bhushan.
-
May 22nd, 2008, 04:27 PM
#2
Re: About abstract/sealed/virtual/override/new keywords...
Let us do it like this. You tell us what answers you have for the questions and then we will give inputs. I guess that way we will come to know about your understanding of all the questions and then clear any misunderstandings.
-
May 22nd, 2008, 05:13 PM
#3
Re: About abstract/sealed/virtual/override/new keywords...
Hi all. I have following simple questions:
<1> What is the difference between abstract class and an interface?
I know the following-
- Fields/member vaiables are allowed in abstract class, where as they are not allowed in an interface.
- The class implementing an interface has to implement all the methods/properties/delegates in the interface, but a class inheriting an abstract class need not implement all the methods/properties/delegates in the base class which is abstract class. But I am not sure if the user has to simply ignore the methods or he has to declare them as abstract in the derived class if he is not going to implement that method?
- No constructors, destructors, garbage collection in an interface.
-----------------------------------------------------------------------------------------------
"Please comment on this one..."{;;}
<2> In an enterprise development, which one comes first - an abstract class or an interface? This was an interview question.
------------------------------------------------------------------------------------------------
"I think that it is the interface first because of the simple fact that the abstract classes can derive from the interfaces and then can implement the interface and all the successive classes might be derived from the abstract class, which gives us the option of including/excluding the methods/properties/delegates/indexers declared in the interface. Also, we can add some fields in the derived abstract class to add functionality it implements. In addition to it, the coder can customize the base functionality provided by the interface for an abstract class."
<3> What is happens when the keyword abstract is used to declare and implement a method? What happens when a keyword sealed is applied to a method?
----------------------------------------------------------------------------------------------
"I am not sure about this. But simply put, an abstract method does not have an implementation. But, if any class, even if it has a single abstract method, has to be declared as abstract. I do not know the purpose of declaring a method abstract in real time applications. If a method is declared as sealed, then it cannot be overridden or newed. I am not sure about this as well.
<4> Can we have virtual/override/new keywords attached to a contructor in C#? If yes, then answer the following:
Code:
class A
{
public virtual A(){ }
}
class B : A
{
public override B(){ }
}
class C : B
{
--------------- C(){ } // Mention the keyword(s) that will come here
// if the base class c'tor is to be invoked and otherwise
}
----------------------------------------------------------------------------------------------
The blank will be public override if a base class constructor has to be invoked and it would be public/private/protected + new if it does not want to invoke the base classes. I need more information on this.
<5> What is the analogous of C# static keyword in VB.NET?
-----------------------------------------------------------------------------------------------
"Shared". However, I asked this because VB.NET has a keyword Static which is used in some other context and I want to know what is that context?
<6> Is there a C# equivalent for keywords My, AddressOf, With in C#? If not then how are the tasks done by them in VB.NET performed in C#?
-----------------------------------------------------------------------------------------------
I do not know about the equivalent keywords. I just know that the things are done a little differently in C#.
<7> Which of the ways is cost effective? Boxing or Unboxing?
-----------------------------------------------------------------------------------------------
I do not know for sure, but boxing causes the casting of the other/simple datatypes such as int/float/double to object types which is inturn involved with the conversion of value types to reference types. Unboxing is reverse of boxing. So, if simple data types are converted to object types there has to be a specific and valid reason for doing so. For instance, if someone has to use the methods of Object class. Otherwise, boxing is not help ful as it only adds to list of operations. And hence it is costly.
Please comment....
Although I have a rough idea of all the Qs, I am not 100% confident about them. So please give all your inputs...
Thankyou,
Bhushan.
-
May 22nd, 2008, 09:00 PM
#4
Re: About abstract/sealed/virtual/override/new keywords...
Why not check your VS documentation or visit the MSDN?
Busy 
-
May 23rd, 2008, 02:00 AM
#5
Re: About abstract/sealed/virtual/override/new keywords...
I'll answer only the most interesting and not trivial question:
 Originally Posted by bhushan1980
<1> What is the difference between abstract class and an interface?
The main difference is that interface respresents loose coupling, while abstract class represents tight coupling.
Other significant difference is that interface declares a contract, while abstract class implies implementation.
Please take this a suggestion to your study (sorry, but your are touching trivia, which cannot be explained in few words).
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
May 23rd, 2008, 10:54 AM
#6
Re: About abstract/sealed/virtual/override/new keywords...
Hi.
Thank you for all the replies. Shuja asked me to mention the things that I know, so I did explain them. Regarding going through Visual Studio documentation, I have gone through it all the time and will continue to do so. I can get all the problems solved there, except a few. Not out of any sort of complacency, I just missed the entire plot to explain myself clearly. What I am looking for here, are the things that are not easily found/understood in the documentation. And I am terribly sorry about that. The questions that I have asked are just to engage into some discussions and I believe that through discussions we come to know a few things that we might have skipped. For instance, the point mentioned by boudino is a good one although I have not looked it up and try to find more on it to discuss. I had once read about COM interop and RCW (Runtime Callable Wrapper) and in that I came across early and late binding of the COM interfaces. But these terms i.e., 'loosely coupled' and 'tightly coupled' are new to me. I will into them for sure. Also, when boudino says that "an interface is a contract and an abstract class implies implementation" do you mean that in a software development world, interfaces are developed according to the strategic software plan where as abstract classes are more of customization and extensions of an interface or they can even be on their own without being derived from an interface.
So, in the end, I urge u all to give me the out of box information that is not easily available on MSDN. Whereever, the entire question or topic does not have anything other than trivial, please type a single word 'TRIVIAL' and I will accept it. I would be more than happy if a member is able respond to (or address) a small part of my queries.
Thanks and Regards,
Bhushan.
P.S.: I am still looking for brief discussions on some of the questions; more importantly <2>, <4>, <6>, <7>.
-
May 23rd, 2008, 01:43 PM
#7
Re: About abstract/sealed/virtual/override/new keywords...
 Originally Posted by boudino
I'll answer only the most interesting and not trivial question:
The main difference is that interface respresents loose coupling, while abstract class represents tight coupling.
Other significant difference is that interface declares a contract, while abstract class implies implementation.
Please take this a suggestion to your study (sorry, but your are touching trivia, which cannot be explained in few words).
Of course that misses the BIGGEST difference. C# alows derivation from only a single class, but from multiple interfaces. So if you create two abstract classes, you can NOT derive a class that implements BOTH of them. However if you have developed two interfaces......
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 23rd, 2008, 03:07 PM
#8
Re: About abstract/sealed/virtual/override/new keywords...
Ahhh! How did we miss that! I knew it as one of the basic differences between the two; yet due to my extra-ordinary talent to forget the most easiest things to remember, I could achieve these levels of incompetence! Again, HAIL THECPUWIZARD!
One Q pops out of my mind though is, why at all do we need nested interfaces?
Regards,
Bhushan.
-
May 23rd, 2008, 07:14 PM
#9
Re: About abstract/sealed/virtual/override/new keywords...
 Originally Posted by bhushan1980
One Q pops out of my mind though is, why at all do we need nested interfaces?
Assuming you mean 'why do we want an interface to inherit from an interface', i think this shows why it's good:
Code:
public interface IEnumerable
{
}
public interface IList : IEnumerable
{
}
A collection can be represented at it's most basic form as a bunch of objects which you can iterate over. This is the 'IEnumerable' interface. This is a contract. It guarantees that you can iterate over these elements.
An IList is another contract. It allows you to access the elements by index, remove elements, add elements etc. In a list, you want to be able to iterate over the elements aswell, so IList will inherit from IEnumerable.
Does that make sense?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
May 23rd, 2008, 07:49 PM
#10
Re: About abstract/sealed/virtual/override/new keywords...
A nested interface is actually:
Code:
interface INest
{
interface IBird
{
void Fly()
}
void Something(IBird bird);
}
Like any other nested construct it is because the inner (IBird) is intended to have meaning only within the context of the outter (INest).
The exact same question would apply to nested classes, or even namespaces themselves... And if you are asking that question...it is time to go back to the basics....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 24th, 2008, 11:19 AM
#11
Re: About abstract/sealed/virtual/override/new keywords...
I am switching (or rather thrown) back and forth into professional world and the so called basics world. Writing a nested class can be as simple as a breeze. But, things climax when we start adding some references and contexts. For instance, as you mentioned, if we can nest an interface inside another, an abstract class being analogous to an interface, can an abstract class be declared and implemented inside an abstract class? If yes, then how can we access the inner abstract class and its members in the derived class?
Code:
public abstract class Outer
{
public String str1;
public abstract void OuterMethod1();
public abstract int OuterMethod2(int X);
public abstract class Inner
{
public String str2;
public abstract void InnerMethod1();
public abstract int InnerMethod2();
}
public String OuterProperty{ get; set; }
}
class Extend : Outer
{
......
......
......
// How do you access the nested abstract class members?
// Or is it that just as any nested class, it is created for use within
// the scope of the outer class? The derived classes need not access it?
}
I understand that, in these lines, I have entered more into trivia. But believe me, there are some questions that are really coming up ahead.
Thankyou,
Bhushan.
-
May 24th, 2008, 12:32 PM
#12
Re: About abstract/sealed/virtual/override/new keywords...
 Originally Posted by bhushan1980
For instance, as you mentioned, if we can nest an interface inside another, an abstract class being analogous to an interface, can an abstract class be declared and implemented inside an abstract class? If yes, then how can we access the inner abstract class and its members in the derived class?
How would either one be useful?
-
May 24th, 2008, 02:10 PM
#13
Re: About abstract/sealed/virtual/override/new keywords...
I do not know Arjay...That is what I am into. Is there or can there be a situation or need where you have to use either of the 2 i.e., a nested abstract class inside an abstract class and an interface inside an interface? I know about the latter one now... as it was explained by TheCPUWizard. Also, I knew about the regular nested classes. But how about the nested abstract classes? I am asking this because, while working with C++, I never came across a need where you have to create nested classes. We can always the create the instances of the classes created outside the class whereever appropriate and desired. The concept of nested class came to me while with C# which has adopted or has tried to adopt majority of OOPs benefits and strengths provided by C/C++ and Java. Here is an MSDN link that explains about nested classes in C++:
http://msdn.microsoft.com/en-us/libr...37(VS.80).aspx
Another good link for C++ is found on IBM website and that is:
http://publib.boulder.ibm.com/infoce...ef/cplr061.htm
Another one showing the comparison of nested classes in C# and Java:
http://blogs.msdn.com/oldnewthing/ar...01/685248.aspx
Hey Arjay check out this one an interesting one:
http://staff.develop.com/jasonw/webl...2/05.html#a151
I still have to complete come to the terms as to how the implementation has worked for the writer...
Again, sorry to bring in C++ into C# forums, but all these things might apply to C# as well, except a few such as including the use of typedefs and pointers in C++ to achieve certain functionality.
Thanks,
Bhushan.
-
May 24th, 2008, 09:15 PM
#14
Re: About abstract/sealed/virtual/override/new keywords...
1) An abstract class is completely different than an interface. (although they are effectively the same in C++, this IS the C# forum).
2) You NEVER need a nested class (abstract or otherwise) nor a nested interface. They are simply constructs that provide "intent".
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 25th, 2008, 02:02 AM
#15
Re: About abstract/sealed/virtual/override/new keywords...
 Originally Posted by bhushan1980
Hi all. I have following simple questions:
<1> What is the difference between abstract class and an interface?
I know the following-
- Fields/member vaiables are allowed in abstract class, where as they are not allowed in an interface.
- The class implementing an interface has to implement all the methods/properties/delegates in the interface, but a class inheriting an abstract class need not implement all the methods/properties/delegates in the base class which is abstract class. But I am not sure if the user has to simply ignore the methods or he has to declare them as abstract in the derived class if he is not going to implement that method?
- No constructors, destructors, garbage collection in an interface. Please comment on this one...
<2> In an enterprise development, which one comes first - an abstract class or an interface? This was an interview question.
<3> What is happens when the keyword abstract is used to declare and implement a method? What happens when a keyword sealed is applied to a method?
<4> Can we have virtual/override/new keywords attached to a contructor in C#? If yes, then answer the following:
Code:
class A
{
public virtual A(){ }
}
class B : A
{
public override B(){ }
}
class C : B
{
--------------- C(){ } // Mention the keyword(s) that will come here
// if the base class c'tor is to be invoked and otherwise
}
<5> What is the analogous of C# static keyword in VB.NET?
<6> Is there a C# equivalent for keywords My, AddressOf, With in C#? If not then how are the tasks done by them in VB.NET performed in C#?
<7> Which of the ways is cost effective? Boxing or Unboxing?
Although I have a rough idea of all the Qs, I am not 100% confident about them. So please give all your inputs...
Thankyou,
Bhushan.
<1> Lots of good anwsers above.
<2> It depends on the actual needs. If you need members and abstract methods, use an abstract class. If you need a contract use an interface. There is no rule to which one comes first - that's why they both exist. Maybe the expected anwser was to first declare interfaces then declare abstract classes that implement those interfaces but again, it all depends on the design.
<3> If you declare a method as abstract, you cannot implement it. You can implement it only on a derived class.
You can use the word sealed on a method declaration when you implement a method declared with the override keywords so that classes that derive from the class where you implemented it (with the sealed keyword) won't be able to override it again - they will be forced to use the sealed implementation.
<4> Those keywords are illegal for constructors. Constructors, by definition, initialize requiered members of a class. When instantiating a derived class, the constructor of the parent class is called implicitly to make sure everything is initialized the way it should - that's why it doesn't make sense to be able to override constructors.
<5><6> Sorry, no VB.NET experience
<7> Boxing and unboxing go hand in hand. Boxing means putting a value type on the heap instead of the stack by treating it like a reference type, unboxing means taking a boxed value and putting it back on the stack. The cost effective way is not using boxing and unboxing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|