welcome folks
FAQ in android,FAQ in dotnet,DotNet, C#,DOT NET CODE, ASP.NET, VB.NET, TRICKS,Dotnetcode,Android and Latest Technology Articles in VB,Android and ASP.NET

Friday, August 31, 2012

Difference Between Abstract Class & Interface in .net?

Abstract Classes:- 

-->abstrct methods means only method is defined.body undefined.(Not implented) just like skelton 

-->non-abstract methods means method body is defined 

just see below.. 

Sample_Parent is one class having 2 methods : 

1--method1()--this is abstract method 
2--method2()--this is non abstract method 

public abstract class Sample_Parent 

public void method1(); 

public void method2() 

// write code here 




the above clss having One abstract method is there,so should class is prefixed with One keyword 'abstract'.otherwise we wil get Compile time error. 

Abstarct class having Some are abstrct methods and some are non-abstrct methods. 

some People like this also Abstact class containg 0 or more number of Abstract methods!! 

Ex:HttpServlet. 

clear right now.. 

--> Now When we wil move For Abstract Class?? 

We have some of the Methods implementation we had..and we dont have some of the methods implementation 

so then this time we use Abstract classes.. 

Suppose i write one example on This situtation:- 

the above class is Abstart class we are unable to create the Object so we have to that abstrct method make it as a Non-abstrct method in Child class. 

Class Sample_child extends Sample_Parent 

public void method2() 

//write code here.. 

public static void main(String args[]) 

//here we have to create Child class object.. 




and illegal combination of abstarct: 

1.private--abstarct classes must inheritd.so the scope must public,default,protected. 
2.sealed Or Final--the abstarct methods are must ovvrride in child classes but final ignored inheritance concept. 
simply abstarct says am implementing in child clss but final is compltly Opposite. 
So these are Illegal combination. 
*************************************************************************************************** 


Now Waht Is Interface:- 

Interface is 100% Abstaract Methods(All are Need to Implement to Child Classes) 


in interface concept there is no chanse of Non-abstract methods. 

and similar to Abstarct classes we are unable to create Object to Interface. 

interface is a keyword.. 

am trying to write a Sample program on interface.. 

interface Sample 

public void Method1(); 
void method2(); 


above Sample is Interface having 2 methods..so thse are ready to implement to in child classes 


public class Sapmle_clss implements Sample 

public void method1() 

//write code here.. 



public void method2() 

//write code here.. 


public static void main(string ar[]) 

//here we have to create object to Class. 



We Dont have Any Implementaion so we move for Ineterface. 

Example: 
Any API's.. 

JDBC--Api relesed by Sun people 
implemnted by OraclePeople,mysql,DB2,etc. 

Servlet API--Relesed by Sun People Implemnetd By Apache,Weblogic, guys,,, 

in interface methods are By default:public, abstrct 
in interface Varibles are By default:public ,static, final 


Advantges Abstract class over Interface:- 

Multiple inheritance Concept not achiving through Abstract Classes 

But Through Interface We can Achive Multiple Inheritance 

No comments:

Post a Comment