Differences between interface and abstract class

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.