Sunday, December 28, 2014

Difference between normal Class and Abstract Class



Base classes  :
1.     Have their own implementations for methods.but we cannot declare method definition without implementation.
2.     Any class can inherit the class and it can be used/added/override to in an inherited class.
3.     You can instantiate a base class

Abstract classes  :
1.     Have declarations for class methods and have implementation of concrete method.
2.     Any class inheriting the abstract class must implement its abstract methods or becomes too abstract itself. Otherwise, it will get error.
3.     You cannot instantiate an abstract class

Example:-

public class A

{

   public void Method1()

   {

     //method code

   }




///Error-'TestConsoleApplication.AAA.Method2()' is abstract but it is contained in non-abstract class 'A'


///public abstract void Method2();



   public virtual void Method3()

   {

    //method code for class A, when class A calls Method3, this code is executed

   }

}



public class B : A

{

           //public override void Method2()
        //{
        //    //this must be implemented here to use it
        //}



   public override void Method3()

   {

     //method code for class B, when class B calls Method3, this code is executed

     //or, if you choose not to override this method, the compiler will use the code in class A

   }

}
class B can still use Method1 from class A because it's inherited. And if class A is to be abstract, then all of the methods would be declared like Method2 and would have to be implemented in class B so that it can be used.



1 comment:

  1. http://www.javatpoint.com/q/4915/what-is-difference-between-normal-class-and-abstract-class-and-interface

    ReplyDelete