Examples of interfaces and abstract class usages in Java _java

Source: Internet
Author: User
Tags abstract

The examples in this article describe the interfaces and abstract class usages in Java. Share to everyone for your reference, specific as follows:

In the object-oriented concept, 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 that we analyze and design in the domain of the problem, the abstraction of a series of seemingly different, but essentially identical, concrete concepts that we cannot instantiate (without a specific object) so called abstractions.

For example: We want to describe "fruit", which 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 take out the only thing to represent the fruit (because apple, oranges can not represent fruit), An abstract class can be used to describe it, so an abstract class cannot be instantiated. When we use a class to specifically describe "Apple", this class can inherit the abstract class describing "fruit", and we all know that "Apple" is a "fruit".

In the object-oriented domain, abstract classes are primarily used for type concealment. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can have any possible concrete implementations. This abstract description is an abstract class, and any possible concrete implementations of this group represent all derived classes of this abstract class.

All abstract methods in interfaces and abstract classes cannot be implemented concretely. Instead of implementing all of the abstract methods in their subclasses (to have the function body, even if {} 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 ways:

Public abstract class AbstractClass//There is at least one abstract method
{public
  int t;//Normal data member public
  abstract void method1 (); Abstract methods, subclasses of abstract classes must implement the abstract method in the abstract class in the class public abstract
  void Method2 ();
  public void method3 (); Non-abstract method public
  int method4 ();
  Publi int method4 () {
    ...///abstract class can give the default behavior of Non-abstract method methods, that is, the concrete implementation of the method
  } public
  void Method3 () {
    ...// In an abstract class, you can give the default behavior of a method that is not abstract, that is, the concrete implementation of the method
  } 


Interface (interface) is defined in the following ways:

Public interface interface
{
  static final int I//interface cannot have ordinary data members, can only have static data members can not be modified, static represents the global, final expression can not be modified, You can implicitly declare the static and final public void method1 () without the static final modification, or
  the method in the interface must be an abstract method, so you do not need to decorate the public void with the abstract
  Method2 (); The default behavior of a method cannot be given in an interface, that is, no concrete implementation of a method
}

In short, abstract classes are functionally incomplete classes, and interfaces are just a collection of abstract method declarations and static data that cannot be modified, neither of which can be instantiated.

In a sense, an interface is a special form of abstract class, in which an abstract class represents an inheritance relationship in which a class can inherit only an abstract class, while a class can implement multiple interfaces. In many cases, an interface can indeed replace an abstract class, if you do not need to intentionally express inheritance on attributes.

To further understand the Java introduction of abstract classes, interfaces to the purpose of the experts to consult the following answers:

1, from the class hierarchy, the abstract class is at the top of the hierarchy, but in the actual design, the general abstract class should be in the back to appear. Why? In fact, abstract class acquisition is somewhat like a mathematical extraction polynomials: Ax+bx,x is an abstract class, if you do not have the previous formula, how do you know X is polynomials? At this point, it is also in line with people's understanding of the world process, first concrete and then abstract. So in the design process, if you get a lot of specific concepts and find their commonalities from among them, this set of commonalities is an abstract class that should be true.

2, interface from the surface, and abstract class is very similar, but the usage is completely different. Its basic function is to assemble some unrelated classes (concepts) together to form a new "new class" that can be centrally manipulated. A typical example I gave my students was "driver". Who can be a driver? Anyone can, just pick up a driver's license. So I don't care whether you are a student, a white-collar worker, a blue-collar or a 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" category, I can specify "driver".

Class Car {
setdriver (driverlicence driver);
}

At this point, the car object does not care what the driver is doing, the only thing they have in common is to obtain a driver's license (both implemented the Driverlicence interface). This, should be the most powerful interface is also an abstract class can not match.

Summarize:

Abstract classes are polynomials that extract specific classes, and interfaces are designed to "hash" some unrelated classes into a common group. Usually we develop a good habit is to use the interface, after all, Java is a single inheritance, unlike C + +, but in the need to use abstract classes must still be used (a bit like the use of Goto), hehe.

I hope this article will help you with Java programming.

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.