/*
Example 1:===================================================================================
How about an analogy: when I was in the Air Force, I went to pilot training and became a Air Force pilot.
At that point I wasn't qualified to fly anything, and had to attend aircraft type training.
Once I qualified, I was a pilot (Abstract class) and a C-141 pilot (concrete class).
At one of my assignments, I was given an additional duty: Safety Officer.
Now I was still a pilot and a C-141 pilot, but I also performed Safety Officer duties (I implemented ISafetyOfficer, so to speak).
A pilot wasn't required to be a safety officer, other people could have done it as well.
All USAF pilots have to follow certain Air Force-wide regulations, and all C-141 (or F-16, or T-38) pilots 'are' USAF pilots.
Anyone can be a safety officer. So, to summarize:
Pilot: abstract class
C-141 Pilot: concrete\base class -- C-141 Pilot IS - A Pilot
ISafety Officer: interface -- C-141 Pilot CAN-DO or CAN BE Safety Officer
added note: this was meant to be an analogy to help explain the concept, not a coding recommendation. See the various comments below, the discussion is interesting.
Example 2:======================================================================================
When to do what is a very simple thing if you have the concept clear in your mind. There is some difference between the two.
Abstract classes can be Derived whereas Interfaces can be Implemented.
When you derive an Abstract class, the relationship between the derived class and the base class is 'is a' relationship.
e.g., a Dog is an Animal, a Sheep is an Animal which means that a Derived class is inheriting some properties from the base class.
Whereas for implementation of interfaces, the relationship is "can be". e.g., a Dog can be a spy dog. A dog can be a circus dog. A dog can be a race dog.
Which means that you implement certain methods to acquire something.
Example3:========================================================================================
Base Class vs Abstract Class vs Interfaces
To answer your question in a very simple manner, consider these:
1) A base class is one which can be inherited by a derived class. Simple!
2) An abstract class IS a base class (and can act as a derived class also by inheriting from a base class), which can be inherited from, BUT cannot be instantiated. Simple!
3) An interface is a type (not a class), which ONLY defines methods (delegates and events) and their signatures. However, its methods CANNOT be implemented.
In general:
1) All methods of an interface MUST be implemeted;
2) Only methods defined as ABSTRACT in an abstract class MUST be implemented;
3) Base classes that are NOT abstract classes can be instantiated or inherited.
Interface:
We do not implement (or define) methods, we do that in derived classes.
We do not declare member variables in interfaces.
Interfaces express the HAS-A relationship. That means they are a mask of objects.
Abstract class:
We can declare and define methods in abstract class.
We hide constructors of it. That means there is no object created from it directly.
Abstract class can hold member variables.
Derived classes inherit to abstract class that mean objects from derived classes are not masked, it inherit to abstract class. The relationship in this case is IS-A.
Example 4:=====================================================================================
> When we should use Interface?
if you don't know about implementation just we have requirement specification then we go with Interface
> When we should use Abstract Class?
if you know implementation but not completely (partially implementation) then we go with Abstract class.
> Interface :every method by default public abstract means interface is 100% pure abstract.
> Abstract :can have Concrete method and Abstract method, what is Concrete method, which have implementation in Abstract class, An abstract class is a class that is declared abstract—it may or may not include abstract methods.
> Interface: We cannot declared interface as a private, protected
Q. Why we are not declaring Interface a private and protected?
Because by default interface method is public abstract so and so that reason that we are not declaring the interface as private and protected.
>Interface method
also we cannot declared interface as private,protected,final,static,synchronized,native.....
i will give the reason: why we are not declaring synchronized method because we cannot create object of interface and synchronize are work on object so and son reason that we are not declaring the synchronized method Transient concept are also not applicable because transient work with synchronized.
>Abstract : we are happily use with public,private final static.... means no restriction are applicable in abstract.
>Interface
Variables are declared in Interface as a by default public static final so we are also not declared variable as a private, protected.
Volatile modifier is also not applicable in interface because interface variable is by default public static final and final variable you cannot change the value once it assign the value into variable and once you declared variable into interface you must to assign the variable.
And volatile variable is keep on changes so it is opp. to final that is reason we are not use volatile variable in interface.
Abstract : Abstract variable no need to declared public static final.
*/