- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Programming with Eclipse-The Best Beginners Tutorial:Abstract class and interface difference
Abstract Class and Interface
Abstract class is very interesting feature in java. It allows you to create skeleton class which contain function definition and description. It can be better understood by the example.
Here you will see that AbstractClass contains full set function with description. This class also contain the get function with function definition. As it does not contain function description so we have to declare it Abstract. Any class that will inherit this have to describe the get function, otherwise it will also be declared abstract.
One point also is that if we write abstract keyword before any function, then other class function can’t override that function. If any class write abstract before it, then other can’t extend it.
Interface
Interface in java is the collection of function definition. In eclipse you go in eclipse file>new>interface.
Create interface with the name employeeinterface.Here you can see that we have only function definition called showText.Now any class can implement it and provide the description of it.Interfaces are very important for long projects as they support reusability.
You can also declare variable in these function. If you see the above program you will notice it, but these variable by default will be constant variable.
Write the above program. Any class that wants to have this functions has to
implements this employeeinterface.That class has to provide implementation of all function.
You can also declare variable in these function. If you see the above program you will notice it, but these variable by default will be constant variable.
Difference between Abstract class and interface
Abstract class can contain function definition and description but interface can only contain function. Abstract class are extended by inherited class but interface are implemented by class.
Variable declared in interface by default is final.Final variable are the variable that can't be changed but variable declared in abstract are not final by default.
Other Java Tutorial You Will Like
- Java Programming with Eclipse-The Best Beginners Tutorial:Part 1
Java with eclipse is the deadly combination.This beginners tutorial will take you through the every detail of java ecplise programming...
- Java Programming with Eclipse-The Best Beginners Tutorial:Overloading And Overriding In Java
Overloading and Overriding in java is big term.Here learn Function overloading and Overring in java.So Read on to build your foundation...