Click to See Complete Forum and Search --> : more "what does this do" questions
Techno
Oct 27th, 2006, 06:51 AM
Sorry, still trying to take advantage of C# and what not.
What does an abstract class do? AFAIK, its like a base a bass class, methods are defined or rather the signature? But there is no actual code and the classes can derrive from this abstract class. Is this correct?
if so, then whats the advantage of this? How would you implement one?
polymorphism - It's been a long time since I heard/used this, I probably do without realizing but can someone tell me what this does and how it benefits us?
protected/internal/virtual/sealed keywords. What do these mean and why are they good/not good and where would you use them? I know using internal classes means that only the classes in the same assembly can access it correct?
Hopefully this should be it and remember, I appreciate each reply. Thanks!
ComputerJy
Oct 27th, 2006, 08:40 AM
What does an abstract class do? AFAIK, its like a base a bass class, methods are defined or rather the signature? But there is no actual code and the classes can derive from this abstract class. Is this correct?No that's not true, Abstract classes can have code in their methods.
For example class BankAccount is abstract and has many sub classes, but they all share the same method of opening and closing an account
ComputerJy
Oct 27th, 2006, 08:46 AM
Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. Polymorphism is essential to object-oriented programming because it allows you to use items with the same names, no matter what type of object is in use at the moment. For example, given a base class of "Car," polymorphism enables the programmer to define different "StartEngine" methods for any number of derived classes. The "StartEngine" method of a derived class named "DieselCar" may be completely different from the method with the same name in the base class. Other procedures or methods can use the "StartEngine" method of the derived classes in exactly the same way, no matter what type of "Car" object is being used at the time.
ComputerJy
Oct 27th, 2006, 08:50 AM
Last question
http://msdn2.microsoft.com/en-us/library/ms173121.aspx
jmcilhinney
Oct 27th, 2006, 06:53 PM
The only thing special about an abstract class is that you cannot create an instance of it directly. You can only create instances of derived classes. I believe I mentioned this in one of your previous threads. Abstract classes are created with the express intention of being base classes. They provide a common set of functionality that all base classes will inherit and/or implement but that functionality alone is not enough for an object, so each base class has to add the rest. Consider a Shape class. You know what a Shape is and all Shapes would have common functionality, like SurfaceArea and Perimeter, but you cannot just draw a Shape. All Shapes are something more specific, like a Circle, Triangle, Square, Polygon, etc. Shape would be an abstract class because you cannot create a Shape object directly. You must create instances of classes that inherit Shape.
This also leads on to polymorphism. You have a Shape class that declares a SurfaceArea member and a Perimeter member. Every class that inherits Shape will override those members and implement them appropriately for that shape. You could define a method with a Shape argument and within it call the SurfaceArea and Perimeter members of that argument. You could then call that method and pass a Circle object, a Triangle object, a Square object or any other class that inherits Shape and it would work. Not just that, even though the parameter is a Shape reference, it would be the SurfaceArea and Perimeter members of the object's actual type that would be invoked.
Note that in the case of the Shape class and its SurfaceArea and Perimeter members, both would be declared as abstract and no implementation provided. That's because there is no common or default way to get those values for a shape. They are always dependent on the specific type of shape, so each derived class would have to provide its own implementation. The fact that the member is declared in the base class does two things. Firstly, because they are members of the base class we can implement polymorphism as described above. Secondly, it forces EVERY derived class to provide an implementation. An abstract class can provide an implementation for methods if it wants. It really depends on the situation and whether a common or default implementation is appropriate. Any class that declares a member but doesn't provide an implementation MUST be declared abstract itself.
Techno
Oct 27th, 2006, 07:24 PM
much appreciated once again. I'll try my best to understand even more about this *brain numbing*
jmcilhinney
Oct 27th, 2006, 07:32 PM
OOP is really not very difficult to understand as long as you keep in mind that it's supposed to mimic the real world. Take polymorphism for example. If someone blindfolded you, gave you a key, led you in a particular direction and said "there's a vehicle in front of you, get in it and start the engine" I'm betting that with a bit fo fumbling around you could get it done quite quickly. It wouldn't matter what type of vehicle it was because you know that all vehicles have basically the same arrangement of doors and ignition. So it doesn't matter what type of object is passed to the method, as long as its type inherits Vehicle you can call its StartEngine member. Everything about OOP is logical, simple and intuitive if you just come at it with the right frame of reference.
Techno
Oct 27th, 2006, 08:18 PM
oh yes I agree I mean I do know and have done and do OO but its just the termonology I was iffy on :)
jmcilhinney
Oct 27th, 2006, 08:42 PM
Sometimes VB says it better, or at least more plainly.
'sealed' in C# = 'NotInheritable' in VB
'virtual' in C# = 'Overridable' in VB
'abstract' in C# = 'MustInherit' for classes and 'MustOverride' for members in VB
Techno
Oct 27th, 2006, 08:45 PM
kewl. thanks a bunch ;) you really should write a book and im not joking :)
Techno
Oct 30th, 2006, 12:50 PM
hmm sorry getting confused once again.
abstract class is/could be a base class? Am I correct?
to override a method in the abstract class, would the abstract method have a virtual decleration? Like:
public virtual void DoSomething();
?
jmcilhinney
Oct 30th, 2006, 04:20 PM
An abstract class CANNOT be instantiated directly. If you want to create an instance then it MUST be inherited, so that you can then create an instance of the derived class. The only methods that you can call on an abstract class that has not been inherited are static methods, which do not require an instance.
An abstract method doesn't have a 'virtual' declaration. It has an 'abstract' declaration. A virtual method is one that CAN be overridden in a derived class. An abstract method is one that MUST be overridden in a derived class, unless the derived class is itself abstract. A virtual method in an abstract class still has a body, while an abstract method never has a body.
Techno
Oct 31st, 2006, 01:29 PM
sorry for being slow but im still trying to grasp this.
Can an abstract class be defined as a base class which other classes can inherit?
still confused between the virtual method on what it does and how it can be used and what its effects are of other classes using it, in comparison to the abstract methods.
I know an abstract class that has methods must be overriden in the classes that it inherits the abstract class? correct? (probably not!)
protected - whats the difference between this and sealed? I know sealed will not allow further classes to override the method of the derrived class?
protected is accessible within its class and by derrived classed but is that not quite what the virtual method is?
sorry, im confused!
ComputerJy
Oct 31st, 2006, 01:55 PM
still confused between the virtual method on what it does and how it can be used and what its effects are of other classes using it, in comparison to the abstract methods.A virtual method can be overridden in subclasses, so it doesn't have to be implemented. For example all dogs have 4 legs, but do they all walk the same way??
No, hence we don't implement the walk method in the super(base) class dog, instead we make a virtual method... why? because it wouldn't make sense for a dog not to walk :)
I know an abstract class that has methods must be overriden in the classes that it inherits the abstract class? correct? (probably not!)Not necessarily, abstract classes might have implemented method. I'll use the same example. All dogs drink the same way, so it would be much easier to implement the drink method in the Dog class instead of writing a drink method for each Dog subclass
protected - whats the difference between this and sealed? I know sealed will not allow further classes to override the method of the derrived class?
protected is accessible within its class and by derrived classed but is that not quite what the virtual method is?I think this link will be helpful http://msdn.microsoft.com/vcsharp/programming/language/default.aspx
jmcilhinney
Oct 31st, 2006, 06:03 PM
Any class that is not declared 'sealed' can be inherited. By not declaring a class 'sealed' you are implicitly saying that that class can be inherited. You don't define any class, abstract or not as a base class which other classes can inherit. A class is a class, whether abstract or not. If it's sealed then it can't be inherited. If it's not sealed it can be inherited. Very simple. Whether or not the class is abstract has no relevance here. Whether or not it is sealed is what counts. A sealed class cannot be inherited and ALL other classes can.
The ONLY thing that differentiates an abstract class from other classes is that you cannot create an instance of it directly. Again, that is the ONLY difference between abstract classes and other classes. If a class is declared 'abstract' you cannot create an instance of that type by invoking its constructor directly with the 'new' keyword. For instance, you cannot create an Animal object directly. You create animals by creating instances of derived classes, e.g. Cat, Dog, Horse, etc.
To summarise:
Sealed classes cannot be inherited.
Abstract classes cannot be instantiated directly.
Very simple.
Now, virtual members are members that CAN be overridden in a derived class. That is the ONLY special thing about virtual members. A member declared 'virtual' in a base class can be overridden in a derived class while other members cannot. That means that a member declared 'virtual' is intended to be a default implementation. Derived classes can choose to accept that default implementation or they can override the member and provide their own implementation.
Abstract members are those that are declared but not implemented. Because there is no default implementation in the base class, all derived classes MUST override the member and provide their own implementation. Any class that contains abstract members must be an abstract class. That's because you cannot create an instance of a type that doesn't have an implementation for all its members.
A class that contains abstract members is automatically an abstract class, but an abstract class doesn't have to contain abstract members. An abstract class is usually an incomplete class. It provides common functionality for all those classes that inherit it, but to be useful those derived classes must add more functionality. That may mean providing an implementation for abstract members, or providing additional members, or both.
To summarise:
A virtual member CAN, but doesn't have to, be overridden in a derived class.
An abstract MUST be overridden in a derived class.
A class containing abstract members must be an abstract class.
An abstract class may, but doesn't have to, contain abstract members.
Now, 'protected' has no connection whatsoever to 'sealed'. If you declare a member 'public' then it can be accessed from outside that class. If you declare a member 'private' it cannot be accessed from outside that class. If you inherit a class, the derived class can see all the base classes public members but none of its private members. If you declare a member 'protected' then it is not accessible from outside the class, just like a private member. The derived class, however, CAN see the protected members, just like a public member. Protected members behave like private members outside the class and its derived classes but like a public member inside the derived classes.
'Protected' has no connection whatsoever to 'virtual'. A virtual member can be overridden in a derived class. A protected member cannot be accessed from outside the class in which its declared or its derived classes. There's no point declaring a virtual member 'private' because if it can't be seen by a derived class then it can't be overridden. A virtual member may be 'public' or 'protected'. A protected member may be declared 'virtual' or not. There is no relationship between the two.
To summarise:
A public member can be accessed anywhere.
A protected member can be accessed only in the class in which it's declared or a derived class.
A private member can be accessed only in the class in which it's declared.
Techno
Oct 31st, 2006, 06:16 PM
thanks so much :D
still grasping protected - whats the point of it? I'm going to re read again..but most of that DID make sense :D
jmcilhinney
Oct 31st, 2006, 06:35 PM
The point of protected is exactly so that a derived class can use and/or override the member without it being seen from outside the class. An example of protected members are the methods that raise events. Take the Paint event of a control. The Paint event is declared 'public' in the Control class. That's because we want to be able to see a control's Paint event from the outside. The OnPaint method, which raises the Paint event, is declared 'protected' and 'virtual'. The OnPaint method is declared 'virtual' so that each control can override it to provide it's own custom painting code. The OnPiant method cannot, therefore, be declared 'private' because it must be accessible to derived classes. We don't want the OnPaint method to be declared 'public' though because we don't want it to be accessible outside the control because it's not appropriate for the OnPaint method to be called directly. How can we declare a member so that it's accessible to a derived class but not accessible outside the class? The answer is to declare it 'protected'.
Techno
Oct 31st, 2006, 07:03 PM
gotcha. Thanks so much for this, really appreciate it.
hopefully, i should not make some examples and demo I can get the grasp of it.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.