Click here to Skip to main content
15,112,304 members
Please Sign up or sign in to vote.
2.08/5 (5 votes)
See more:
Hi...
I need to know the difference between normal class and abstract class and also where and why we need to use abstract class...its better to explain with simple real time example
Posted
Updated 12-Dec-17 11:59am
Comments
   
It is impossible to explain without knowing your background. What prevents you from reading documentation on this simple aspect of programming? Do you understand inheritance? If yes, now abstract could be not obvious? If no, do you want us to write a book for you called "Object-oriented Programming"? :-)
--SA
Priyaaammu 28-Jun-12 1:09am
   
i dont want you to write a book for me...what prevents you to tell the difference in that. I asked to know whether i understand it correctly or not,how do you decide that i didnt read anything? If u cant explain,just leave it...

Please see my comment to the question.

First of all, the question of the kind "what is the difference between {0} and {1}" cannot be valid. If this does not seem apparent to you, please tell us: what's the difference between apple and Apple? If you still did not get it, please read my past answer: what is the difference between the class and encapsulation in programming[^].

Also, it would be good to know what do you mean by "normal class"?

If you want to learn just the basics of programming, the most important skill is explained here:
Microsoft Q209354.

More specifically, you should understand this:
http://en.wikipedia.org/wiki/Abstract_class[^].

In particular, as it is done in .NET, it's well explained here, with code samples:
http://msdn.microsoft.com/en-us/library/ms173150.aspx[^].

Any questions?

—SA
   
v2
Comments
Liju Sankar 3-Feb-15 13:06pm
   
+5
   
Thank you.
—SA
refer any good book of c++. you got a lot of real time example and differences.

go through these link -
abstract class [^]

OR
abstract class in java[^]
   
Making a class abstract allows you to use abstract functions. Abstract functions allow you to guarantee that children/subclasses will have these functions on them. The abstract class will not be instantiatable (can't exist "in the world").

Example:
C#
public abstract class Dog
cannot exist via
C#
Dog dog = new Dog()
but a subclass
C#
public class GreatDane : Dog
can exist in the world.
C#
GreatDane greatDane = new GreatDane()


Why? It makes no sense to create a generic "dog" in the world. Being abstract stops it being created. It also allows you to have abstract function template/stubs that all children/subclasses must have.
C#
public abstract class Dog
{
    public abstract void Bark(); // Note there is no code/implementation. I am just telling my children to have this function on them.
}

C#
public class GreatDane : Dog
{
    public override void Bark()
    {
        // Unique bark like a Great Dane
    }
}


Why not use an Interface like IBarkable?
You could, but having the base class "Dog" allows for shared/common behaviour. You could have regular functions, virtual functions or abstract functions.

For example, if 90% of dogs bark the same way, you can make Bark() virtual, so there is default/shared/common implementation.
C#
public abstract class Dog // Abstract so it can't be instantiated, even though there are no abstract functions
{
    public virtual void Bark()
    {
        // Default (but overridable) barking code. Some dogs use this code.
    }

    public void WagTail()
    {
        // Only code that exists to wag tail. All dogs use this code.
    }

}

C#
public class GreatDane : Dog
{
    // I will be lazy and use the base class's Bark()
}

C#
public class Rottweiler : Dog
{
    public override void Bark()
    {
        // I will use this unique bark.
    }
}

With interfaces, every Bark() would be unique, leading to copy & pasted code.
   
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900