Abstract Class vs Interface
A class can implement any number of interfaces but a subclass can at most inherit from only one abstract class.
An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract.
An abstract class can declare or use any variables while an interface is not allowed to do so.
following Code will not compile
An abstract class can have constructor declaration while an interface can not do so.interface TestInterface{int x = 4; // Filed Declaration in Interfacevoid getMethod();string getName();}
An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface we can not declare any access modifier(including public) as all the members of interface are implicitly public.
Although it is perfectly legal to make interface access specifier as Public (Remember only public is allowed)
Below code is correct :
public interface TestInterface
{
void getMethod();
string getName();
}
Below code is wrong:
interface TestInterface
{
public void getMethod();
string getName();
}
No comments:
Post a Comment