Java Predefined-Functional Interfaces
Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.
You can also define your own custom functional interface. Following is the list of functional interface which are placed in java.util.function package.
Supplier, Consumer , Function Predicate etc.
Interface is a group of related methods with empty bodies(Signatures). It is not a class. One class may implements multiple interfaces thus helps to achieve multiple inheritance. All the methods in interface are by default abstract with out any abstract key word.
Examples for interface :
interface vehicle {
void startVehicle(Sting commandToStart);
void stopVehicle(String commandToStop);
void speedUp(int speed);
}
to implement the above interface we can have mutiple classes like Bycycle, MotorBike, Car etc.
class MotorB{
}
Abstract Class :
A class declared as abstract - It may or may not contain abstract methods. It can't be instantiated but can be subclassed. Means an abstract class can only be subclassed which mean it can be inherited from. In simple words other classes can inherit an abstract class but they can't initiate the abstract class. One class can extend only one abstract class.
Abstract method :
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:Abstract classes are classes that contain one or more abstract methods.
Example:
public abstract class vehicle{
//Non abstract method
void int speedUp(int speed){
spedd = speed + 10 ;
}
abstract void stopVehicle(String commandToStop);
}
public class thunderBird extends vehicle{
void stopVehicle(String commandToStop)
{
commandToStop;
}
}
So in the above abstract class vehicle there are two methods one is speedUp which is non-abstract method and one abstract method stopVehicle . So the classes that extend this abstract class must define the abstract methods declared in abstract class other wise the class itself must declared as abstract class again.
An abstract method is a method that is declared, but contains no implementation.
Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
An Abstract class without any implementation just looks like an Interface.
Abstract class can implement interface ..
abstract class jumbo implements vehicle {
void startVehicle(Sting commandToStart){
start ;
}
void stopVehicle(String commandToStop){
stop ;
}
void speedUp(int speed){
speed = speed + 20;
}
}
Refer some more pages on the same topic here [1][2][3][4]
4 comments to "Interface Vs Abstract class Java"
Post a Comment
Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
-
Following are some of interesting blogs I found till now ...please comment to add your blog here. Blogs in English : http://nitawriter.word...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs
Anonymous says:
Pure abstract class is an interface :)
Anonymous says:
nice blog boss....!
JP @ abstraction in java says:
Indeed you have done a great job on differentiating the interface and abstract class in java . just to add when we use abstract class we can not extend another class but with interface we have that space spare with us. just consider if Runnable were an abstract class instead of interface, how inflexible it would be.
Javin
How HashMap works in Java
Anonymous says:
Thanks Krishna! This helped my understanding a lot. This site also helped me very much with regard to interface vs abstract class:
http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/