Interface: Interface
Abstract class: abstract class
Class: class, including abstract classes.
Java learners, or oo programmers, need to figure out the differences between the two concepts.
Let's talk about my point of view.
In OO, interface and abstract class are two different concepts.
1. Abstract class is similar to abstract data type (abstract data type). It defines all of its subclass (subclass) features and declares methods and variables in abstract class. For example, define an abstract type: vehicle. In Java, the abstract must be added to the class or method. For example:
Public abstract class car
{
Public int wheel;
Public abstract void mobile ();
}
This abstract class declares all features (such as wheels) and actions (such as movement) that a car should have ). Then you can extend the abstract class extend, as long as it is a car, you can also add your own features in the subclass (subclass), such
Public class sports car extends
{
Public void mobile ()
{
System. Out. println ("I'm moving ");
}
Public void engine type () // behavior of the self (sports car)
{
System. Out. println ("advanced engine ");
}
Wheel = 4;
}
Public class bicycle extends car
{
Public void mobile ()
{
System. Out. println ("I'm moving ");
}
Public void () // behavior of self (bicycle)
{
System. Out. println ("I Can't move ");
}
Wheel = 2;
}
2. Interface is used to define a protocol, and all methods can implement it. An interface defines methods and final static variables. All implementations of this interface must ensure that the implementation is consistent with the behavior defined by this interface. Using Interfaces allows objects to communicate with each other under certain specifications.
For example
Public interface TCP protocol
{
Public void open port ();
Public void close port ();
}
In this case, the two methods can be used for communication as long as the interface is implemented.
On the Java platform, there are three main differences:
1. A class can inherit only one super class, but multiple interfaces can be implemented.
2. In abstract class, you can have non-Abstract (abstract) methods, that is, meaningful methods, but interface cannot.
For example
Public abstract class car
{
Public int wheel;
Public abstract void mobile ();
Public String what am I ()
{
Return "car ";
}
}
3. The interface does not belong to the inheritance scope. That is, it is impossible to become a super class)
(End)
This article is based on the http://access1.sun.com/FAQSets/newtojavatechfaq.html's q13 in Sun's FAQ
Translated