Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is the difference between an abstract class and normal public class?

Note:
I've seen many difference but i need to know the "use of inheriting an abstract class" while "inheriting a public class" does same functionality.
From Below example,
1.Want to know difference between( C : A and C:B )

What I have tried:

public abstract class A
{
//some members;
}

public class B
{
//some members;
}

public class C : A
{
// accessing the members of "A";
}


public class C : B
{
// accessing the memebers of "B"
}
Posted
Updated 3-Aug-16 19:33pm

The difference isn't in the class C - it's in the nature of an Abstract Class vs a "normal" class: An Abstract class cannot be instantiated, and a concrete class derived from an Abstract class must implement all abstract members of the base class.

All that means is that you can't do this:
C#
public abstract class A
    {
    }
...
    A a = new A();
Because the compiler will complain that A is not a concrete class.
This is really useful when you need a class to provide common behavior, but that is useless as an instance on it's own.
For example you might have an abstract Car class which declares an abstract Drive method, and the derived manufacturer model classes must all implement it. You can Drive any Car based instance, but to get a vehicle you can actually buy, it needs to be a FordFiesta, or a MercedesAClass, or a MiniCooper. These are the "concrete classes" of Cars for which instances can be created.
 
Share this answer
 
Comments
Member 12667612 3-Aug-16 7:05am    
thank you for your solution sir, but i need to know if the public class and abstract class does same functionality when inheriting to other class, then what is the use of abstract class.
OriginalGriff 3-Aug-16 7:07am    
Read what I said!
I am going to answer your question in the point of view of "Why do we use abstract class".

Abstract class has the following features in compare to a non-abstract class("normal class" is what you are saying or Interface).

An abstract class can have both abstract method and non-abstract method, Eg -
C#
abstract class ExampleClass
      {
            //This is a Non-abstract method
            public int Add(int Num1, int Num2)
            {
                return Num1 + Num2;
            }

            //This is an abstract method
            //This method will be overridden in the derived class
            public abstract int Subtract(int Num1, int Num2);
      }


The functionality of the Abstract classes can be extended further without changing it's child classes.
Consider that you have an Interface in your project called IExample. Now, if you are making any changes to this Interface, all its child classes will have an effect.

Abstract classes are very useful when you required your base class to provide the default implementation of certain methods whereas other methods are overridden by child classes. Using interface has more restriction, whereas Abstract class provides mixture of ways to do things.

Finally, the full feature of the Abstract class can only be understood only by implementing this in a project design and experimenting, rather than trying to understand theoretically.
 
Share this answer
 
v2
Comments
ZurdoDev 3-Aug-16 10:10am    
An interface is not a normal class. An interface is similar to an abstract class. Classes can implement interfaces, but an interface is certainly not a normal class.
Swinkaran 3-Aug-16 18:39pm    
Thanks Ryan for correcting, I accidentally included it. Updated.
Abstract class is half defined class where we define abstract methods which will be implemented by child classes.
We should use abstract classes for following reason:
1. To keep some common code in base class
2. To enforce rules in your class hierarchy that your child classes will follow and implement abstract methods
 
Share this answer
 

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