Java abstract classes and interfaces

Source: Internet
Author: User

In object-oriented concepts, we know that all objects are depicted by classes, but not all classes are used to depict objects, 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 we derive in the analysis and design of the problem domain, as an abstraction of a series of concrete concepts that look different, but are essentially the same, and we cannot instantiate them (not get a concrete one) so they are called abstractions. For example: We want to describe "fruit", it is an abstraction, it has quality, volume and other common (fruit quality), but also lack of characteristics (apples, oranges are fruits, they have their own characteristics), we can not get the only one to represent the fruit of the thing (because apples, oranges can not represent fruit), Abstract classes can be used to describe it, so abstract classes cannot be instantiated. When we use a class to describe "Apple", this class can inherit the abstract class describing "fruit", we all know that "Apple" is a kind of "fruit".

Abstract classes are primarily used for type concealment in the object-oriented realm. We can construct an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all derived classes of this abstract class.

All abstract methods in interfaces and abstract classes cannot be implemented, but all abstract methods should be implemented in their subclasses (to have the function body, even if the {} is empty), the Java designer may consider the flexibility of the abstract method, and each subclass can implement the abstract method according to its own needs.

Abstract classes are defined in the following way:

Public abstract class AbstractClass//Inside there is at least one abstract method

{

public int t; Ordinary data members

private int i; Private data members

public abstract void method1 (); Abstract methods, subclasses of abstract classes must implement abstract methods in an abstract class in a class

public void method3 (); Non-abstract methods

public int method4 ();

public int method4 () {

...//abstract class can be given the default behavior of non-abstract method methods, that is, the concrete implementation of the method

}

Interface (interface) is defined in the following way:

Public interface Interface {

static final int i=1; The interface cannot have ordinary data members, can only have static data members that cannot be modified, static represents the global, final represents non-modifiable, and can be implicitly declared static and final without static final decoration. Initial value must be assigned

public void method1 (); The method in the interface must be an abstract method, so no abstract modification is necessary. There cannot be a method body.

}

  

  In short, an abstract class is a functionally incomplete class, and an interface is simply a collection of abstract method declarations and data that cannot be modified statically, neither of which can be instantiated. In a sense, an interface is a special form of abstract class, in the Java language the abstract class represents an inheritance relationship, a class can inherit only one abstract class, and a class may implement multiple interfaces. In many cases, an interface can actually replace an abstract class if you don't have to deliberately express inheritance on the property.

 

Further understanding

1, from the hierarchical structure of the class, the abstract class is at the top of the hierarchy, but in the actual design, generally speaking, abstract classes should be in the back to appear. Why? In fact, abstract class acquisition is a bit like the extraction of mathematics common divisor: ax+bx,x is abstract class, if you do not have the previous formula, how do you know if x is common divisor? At this point, it is also in line with the process of understanding the world, first concrete and then abstract. So in the design process if you get a lot of concrete concepts and find their commonalities from them, the set of commonalities is that abstract classes should be right.

2, interface on the surface, and abstract class very similar, but the usage is completely different. Its basic function is to bring together some unrelated classes (concepts) to form a new, centrally manipulated "new class". One of the typical examples I give to students is "driver". Who can be a driver? Anyone can, just pick up the driver's license. So I don't care if you are a student, white collar, blue collar or boss, as long as you have a driver's license.

Interface Driverlicence {

Licence getlicence ();

}

Class Studentdriver extends Student implements Driverlicence {}

Class Whtiecollaremployeedriver extends Whtiecollaremployee implements Driverlicence {}

Class Bluecollaremployeedriver extends Bluecollaremployee implements Driverlicence {}

Class Bossdriver extends Boss implements Driver {}

When I define the "car" class, I can specify "driver".

Class Car {

Setdriver (driverlicence driver);

}

At this time, car objects do not care what the driver is doing, the only thing they have in common is to get a driver's license (all implemented the Driverlicence interface). This, should be the most powerful place of the interface is also the abstract class incomparable.

Summary: Abstract classes are common divisor that extract specific classes, and interfaces are meant to "hash" some unrelated classes into a common group. Usually we develop a good habit is a multi-use interface, after all, Java is a single inheritance, not like C + +, but in the need to use abstract classes must still be used

Java abstract classes and interfaces

Related Article

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.