Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by bhushan1980
<2> In an enterprise development, which one comes first - an abstract class or an interface? This was an interview question...
Thats IMHO a question pointing to design pattern rules. And here we have clearly said that using compositions is prefered before inheritance. This would lead to the following decision: If you can choose to use an abstract class or an interface to implement a specific requirement then choose the interface implementation.
Re: About abstract/sealed/virtual/override/new keywords...
You are right, I've forgotten it. Maybe because I strongly reject to use inheritance as main design approach. I prefer interfaces and composition, so I very rarely come accross the lack of the limitation of inheritance to just one predecestor
Re: About abstract/sealed/virtual/override/new keywords...
Hi. One more addition here. I searched on google and I came to know that we can have an implementation of a function inside an abstract class as well! So first of all let me know if this is true? And if it is, then please tell me how can we call or interact with that function?
Thankyou,
Bhushan.
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by bhushan1980
Hi. One more addition here. I searched on google and I came to know that we can have an implementation of a function inside an abstract class as well! So first of all let me know if this is true? And if it is, then please tell me how can we call or interact with that function?
Sure. An Abstract Class must have at least one Abstract Method, it can have zero or more "Concrete" methods (ie It does not have to be "Pure"). This is the advantage of an Abstract class over an interface.
IF an abstracxt class had to be pure, then it would only have the capabilities of an interface, but with additional restrictions....now very useful... :rolleyes:
Re: About abstract/sealed/virtual/override/new keywords...
Suppose I am implementing an interface in the .NET framework directly and not inheriting from the implementing class in the .NET framework, then as per the rule, do I have to provide an implementation for all the methods? Say for instance:
Code:
// If I am implementing an interface for text-box as
public class MyTextBox : ITextEditor
{
.....
}
An interace like ITextEditor has a lot of methods and properties. So am I supposed to implement all of them?
Thankyou,
Bhushan.
Re: About abstract/sealed/virtual/override/new keywords...
edit: Me Bad..See Reply #23.... :blush: :blush:
EVERY concrete class MUST provide an implementation of EVERY method and property, either explicitly or in one of it's base CLASSes.
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by bhushan1980
...class as well! So first of all let me know if this is true? And if it is, then please tell me how can we call or interact with that function?
Simple try it. You will see that you can use it the same way as you are using methods of an inherited class. Here is an example
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPatternsChapOne {
// Strategiepattern
public interface IWeaponBehaviour {
void UsedWeapon();
}
public abstract class Hero {
public IWeaponBehaviour _weapon;
public void Fight() {
_weapon.UsedWeapon();
}
public void SetWeapon(IWeaponBehaviour wp) {
_weapon = wp;
}
}
// and here the implementations
public class Messer :IWeaponBehaviour {
#region IWeaponBehaviour Member
public void UsedWeapon() {
Console.WriteLine("This is a knife for cutting and stick someone.");
}
#endregion
}
public class BowAndArrow : IWeaponBehaviour {
#region IWeaponBehaviour Member
public void UsedWeapon() {
Console.WriteLine("The arrow goes directly to the heart. ( If you dont miss the target )");
}
#endregion
}
public class Axe : IWeaponBehaviour {
#region IWeaponBehaviour Member
public void UsedWeapon() {
Console.WriteLine("This is a very heavy thing to hit. Orks are using that.");
}
#endregion
}
public class Sword : IWeaponBehaviour {
#region IWeaponBehaviour Member
public void UsedWeapon() {
Console.WriteLine("To use a sword is a special art.");
}
#endregion
}
public class Szepter : IWeaponBehaviour {
#region IWaffenverhalten Member
public void UsedWeapon() {
Console.WriteLine("If it is heavy enough you may kill the person by a hit of its weight.");
}
#endregion
}
// und die Figuren
public class King : Hero {
public King() {
_weapon = new Szepter();
}
}
public class Queen : Hero {
public Queen() {
_weapon = new BowAndArrow();
}
}
public class Troll : Hero {
public Troll() {
_weapon = new Axe();
}
}
public class Knight : Hero {
public Knight() {
_weapon = new Sword();
}
}
}
And you can call this in any method like this
Code:
King myKing = new King();
myKing.Fight();
myKing.SetWeapon(new Messer());
myKing.Fight();
You will see the king changes his weapon. This is a very useful pattern. Your interface may contain 20 methods. You implement them in a seperated class.like we have a knife class, a sword class... you may have lots of such classes each contains the same methods and properties but each for a different purpose.
You have a method in your abstract class calling the methods of your interface. So here an abstract class work together with an interface :D
Very funny, very clever :lol: :lol: ;)
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by TheCPUWizard
Sure. An Abstract Class must have at least one Abstract Method, it can have zero or more "Concrete" methods
Maybe in C++, but in C#, it definitely has not.
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by boudino
Maybe in C++, but in C#, it definitely has not.
You are right boudino, look at my workable example in post #22 it has no abstract method in the abstract class.
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by boudino
Maybe in C++, but in C#, it definitely has not.
Please Reread Post #21.... :D
(Thanks) :wave: :wave:
Re: About abstract/sealed/virtual/override/new keywords...
bhushan1980 == (George2 with humanity upgrade 1.0 installed)? :eek: :ehh: :D
Re the abstraction argument; indeed.. abstract merely requires that a subclass either provide an implementation of that wich is abstract, or defer implementation to antoher subclass
i.e. marking a class as abstract prevents it from being instantiated directly. A subclass must be created
Marking a method as abstract naturally causes the class to become abstract, because the method is not implemented, the class does not represent a complete workable block of code and hence cannot be created.
It thus follows that a class can be abstract (forcing the developer to subclass it in order to instance it) even if none of its members are abstract.
I do think the compiler error "Class XYZ must be marked abstract: it does not implement ABC" is quite succinct at explaining all this, but maybe thats just me
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by boudino
You are right, I've forgotten it. Maybe because I strongly reject to use inheritance as main design approach. I prefer interfaces and composition, so I very rarely come accross the lack of the limitation of inheritance to just one predecestor
Interesting comment, given that every class you use while youre coding, is way down an inheritance hierarchy already.. ;)
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by bhushan1980
What is the difference between abstract class and an interface?
abstract classes can have some ability to function (because they can contain implementatin code), interfaces have no functional code at all. they mostly define what comes in and what goes out so the compiler can do some compile time type checking ;) and other developers know what kind of data to pass around
Quote:
In an enterprise development, which one comes first - an abstract class or an interface?
This question doesnt make sense to me.. It's like asking what comes first, a shopping list (interface - defines what goes in and out, doesnt impl) or a box of ready-made cake mix (abstraction of a cake - just add milk). I never needed one before the other.
Quote:
What is happens when the keyword abstract is used to declare and implement a method?
Nonsense question: You cant implement a method that is marked abstract
Quote:
What happens when a keyword sealed is applied to a method?
You cannot override that method witha new implementation in a subclass
Quote:
Can we have virtual/override/new keywords attached to a contructor in C#?
Constructors dont really partake in all the usual inheritance shenanigans. They behave like they are new, which is why you must explicitly state base(args) when you want to call the parent cosntructor. You should be aware that if you do not provide a constructor, C# provides a default one (literally inserts it into your code when compiling) that takes no args and calls the base() again with no args. If no base constructor takes no args then you must provide a constructor to get around this behaviour because the error indicating the base class has no matching constructor, is seen.
This is not constructor inheritance..
Quote:
What is the analogous of C# static keyword in VB.NET?
shared. vb.net's static is for method variables that retain their value between calls, a somewhat dumb and clumsy idea that c# ditched. if you need the functionality, use a class level variable instead; it's how the vb.net compiler fudges it behind the scenes
Quote:
Is there a C# equivalent for keywords My, AddressOf, With in C#?
this, (not needed; methods can be treated like objects when dealing with delegates), using
read this: http://www.harding.edu/fmccown/vbnet...omparison.html
here's attaching an eventhandler in c#:
obj.Event += Handler(methodName)
vb:
AddHandler obj.Event, AddressOf methodName
Quote:
Which of the ways is cost effective? Boxing or Unboxing?
neither
Re: About abstract/sealed/virtual/override/new keywords...
Quote:
Originally Posted by cjard
a box of ready-made cake mix (abstraction of a cake - just add milk).
I always like the cake mix that comes with frosting. I'm not sure if it's IFrosting or FrostingBase, but it's good.
Re: About abstract/sealed/virtual/override/new keywords...
/me munches on a bowl of Reduced Sugar Kelloggs Frosties* and wonders the same thing
*the concept of which i find one of the most amusingly pointless in the breakfast cereal world.. Frosties = cornflakes + sugar, so if you want frosties without sugar.. ;)