Question:
Why there is need to create abstract class?
Prashant
2012-12-11 04:53:45 UTC
I'm new to. Net framework and have small idea about abstract classes but always ive question in my mimd why it is needed to create abstract class...? Plz make me understand through examples...
Thank u
Three answers:
icefyre
2012-12-19 02:16:09 UTC
Answer is you don't have to have abstract classes, just like you don't need to have private member variables. The reason these mechanisms exist is to make sure you and more importantly other programmers use your classes correctly and don't break anything by accessing and doing thing that the class wasn't meant for. Languages like Python do away with many of these mechanisms and just assume that people writing code are "Consenting adults" and will understand how to use a class before they mess around with it. In python the convention is that anything named with an underscore in front (_myprivate) shouldn't be accessed but this isn't enforced by the language. The common mistake is to think that these mechanisms keep your code more secure which isn't true. Anyone can go in and change you class implementation if they have access to the code the idea is to prevent mistakes and help clarify how the class should be used.



Hope this helps!
deonejuan
2012-12-11 07:52:50 UTC
I don't do .net, but Java has always had abstract classes. In the viewpoint of OOP abstract gives you this advantage...



abstract class Fish {

...

abstract void speak() { }

////

public class SaltwaterFish extends Fish{

...

public void speak() {

I live in the ocean.

}

////////

public class FreshWaterFish extends Fish {

...

public void speak() {

I live in the river.

}

/////

public class Trout extends FreshWaterFish{

...}

public class Tuna extends SaltWaterFish{

...}

//// driver class

public class Aquarium {

// and here is the advantage

Fish[] fishes = new Fish[ 5 ];

fishes[0] = new Trout();

fishes[1] = new Tuna();

fishes[2] = new Tuna();

...

// and then, your for-loop can call a single, common method over TYPES

for( Fish f : fishes )

f.speak();



There are other reasons for abstract, but the above example is one everyone can see easy.
?
2012-12-11 05:07:44 UTC
Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes.



Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods.



Examples :

http://www.dotnetperls.com/abstract

http://www.programcall.com/27/csnet/abstract-classes-and-abstract-methods-in-csnet.aspx

http://msdn.microsoft.com/en-us/library/ms173150.aspx


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...