The difference between abstract and interface in Java program Development _java

Source: Internet
Author: User

Let's start with a basic concept.

In the Java language, abstract class and interface are two mechanisms that support the definition of abstract classes. It is because of the existence of these two mechanisms that gives Java a powerful object-oriented ability. Abstract class and interface have a great similarity in support of the definition of abstract classes, and can even be substituted for each other, so many developers are more relaxed about the choice of abstract class and interface when they are defining abstractions. In fact, there is a big difference between the two, for their choice even reflects the nature of the problem areas of understanding, the design of the intention to understand whether the correct and reasonable.

Abstract class and interface are all used in the Java language for abstract classes (the abstract classes in this article are not translated from abstract class, which represents an abstract body, and abstract Class is a method for defining abstract classes in the Java language, which readers should be aware of to differentiate, so what is an abstract class, and what benefits can be derived from using abstract classes?

In the object-oriented concept, we know that all objects are depicted by a class, but that is not the case in turn. Not all classes are used to depict objects (materialization of classes), and if a class does not contain enough information to depict a specific object, such a class is an abstract class. Abstract classes are often used to characterize the abstract concepts that we analyze and design in the domain of problems, and are abstractions of a series of specific concepts that look different, but are essentially the same. For example: If we do a graphic editing software development, we will find that there are problems in the field of circles, triangles and some specific concepts, they are different, but they all belong to the shape of a concept, the concept of shape in the problem area does not exist, it is an abstract concept. It is because abstract concepts do not have specific concepts in the domain of the problem, so abstract classes that represent abstract concepts cannot be instantiated.

Java development often uses virtual functions and interfaces, what is the difference between the two?

Abstract: Subclasses can only inherit one parent class

Interface: Subclasses can implement multiple interfaces

When would you use abstract, and when would you use interface?

A subclass of an Asian can inherit a person's parent class when it is a relationship between a subclass and a parent, such as if an Asian person is a person.
When the subclass and the parent are like a relationship, such as a Superman, Superman's ability is not everyone's, so not everyone's attributes, we can not attribute the ability to the person's parents, then we could use interface

code example:

Public abstract class Mans {public
abstract void Eat ();
} 
Public interface Superpower {public
void superpower ();
}
public class Asiaman extends mans { 
@Override public
void Eat () {
System.out.println ("Asian people eat with chopsticks");
}
Public 
class Americansuperman extends mans implements superpower {public
void superpower () {
System.out.println ("American Superman Can Fly");
@Override public
void Eat () {
System.out.println ("Americans eat with knives and forks");
}
public static void Main (string[] args) {
mans Anasiaman = new Asiaman ();
Americansuperman Anamericansuperman = new Americansuperman ();
Anasiaman.eat ();
Anamericansuperman.eat (); 
Anamericansuperman.superpower ();

Run Result:

Americans eat with knives and forks
American Superman can Fly
Asian people eat with chopsticks

Correct use of abstract and interface is advantageous to improve the readability of the code, when people see extend will know that this is a what, see implement can know what the characteristics of this class like a

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.