Wednesday, 9 December 2015

Difference Between Interface And Abstract class


What is Abstraction

Abstraction is a process of hiding the Complexity and showing only the functionality.

For Example

The Internal details of mobile Or TV remote is hide and we have only the buttons to operate the TV or Mobile
So we know what buttons do but do not know how it does ...

Advantages

We can increase security
------------------------------------------------------------------------------------------------------------

What is abstrct class

1) A class that is declared as abstract is known as abstract class.
2) A Abstract class can not be instantiated Because constractors are not inherites in subclass.

Example Of abstract class




In Following example, Company is the abstract class that contains only one abstract method empSal. 
It implementation is provided by the Employee class



abstract class Company{
abstract void empSal(); 



class Employee extends Company{ 
void empSal(){System.out.println("high salary");} 
public static void main(String args[]){ 
Company c = new Employee(); 
c.empSal(); 





Output is:
high salary

What is Interface....

An interface is a specification of method prototype 


It is blueprint of class .

The interface is a mechanism to achieve the 100% Abstraction in java


Interface is written when all features are implemented differently in different object 


Interface fields are public, static and final by default


methods are public and abstract.

------------------------------------------------------------------------------------------------------------


abstract class Vs interface

                                                               1
If we do not know anything about implementation just we have required specification Then go with interface

If we  are talking about implementation but not completly(partial implementation) THEN we should go for abstract class
                                                                2
Methoda are public and abstract means 100% anstraction

Methods are abstract and concrete both
                                                               3
We can not declare interface method as private ,protected,final static nativ syncronized

There is no restriction abstract class method modifier
                                                              4
Variables are public static and final

No need to be public ,static and final in abstract class
                                                             5
Inside interface we can not declare instance and static blocks otherwise we will get complie time error

We can declare instance and static block inside abstract class 

No comments:

Post a Comment