Java Abstract and interface parsing

Source: Internet
Author: User
Tags abstract definition

Java abstract and interface parsing abstract definition

In object-oriented concepts, all objects are described by a class. However, in turn, not all classes are used to describe objects (materialization of classes). If a class does not contain enough information to describe a specific object, then the class is an abstract class.

For example, if we develop a graphical editing software, we will find that there are some specific concepts in the problem domain, such as circles and triangles, which are different, but they all belong to the concept of shape. The concept of shape does not exist in the problem domain, it is an abstract concept. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain.

In the object-oriented domain, abstract classes are primarily type-hidden. We can construct an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation method. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all possible derived classes.

Semantic analysis of abstract and interface
abstract class Demo{    abstract void method1();    abstract void method2();    void method3(); }

Abstract Demo

interface Demo{    void method1();    void method2();    void method3(); }

Interface Demo

    1. The abstract class can have its own data members, or it can have non-abstract member methods. The interface class can only have static data members that cannot be modified (must be STATCI final, but generally do not define data members in interface), and all methods are abstract.
    2. Abstract class represents an inheritance relationship in which a class can only use one inheritance relationship and one class may implement multiple interface.
    3. In the definition of abstract class, we can give the method the default behavior. However, in the definition of Insterface class, the method cannot have the default behavior.
Analysis of the difference between abstract class and interface from the design level

Abstract class embodies an inheritance relationship in the Java language, and the parent class is essentially the same as the derived class concept, and there is a "is-a" relationship. For interface, it is not required that the Insterface and Insterface definitions are consistent with the concept in nature, but that only the Insterface defined contract is implemented, and the relationship is more similar to the "Like-a".

Example: a door that has inherent behavioral methods, open and close, and also contains the behavior method of the alarm, alarm. Therefore, the understanding of the problem domain is that Alarmdoor is inherently door in concept and has an alarm function at the same time. The design is also obvious, the abstract class represents the inheritance relationship in the Java language, and the inheritance relationship is essentially a "is-a" relationship. So for the concept of door, we should use the abstract class approach to define it. In addition, Alarmdoor also has the alarm function, indicating that it can complete the alarm concept defined behavior, so, the alarm concept can be defined by interface. The code is as follows:

abstract class Door{    abstract void open();    abstract void close();}interface Alarm{    void alarm();}class AlarmDoor extends Door implements Alarm{    void open(){//implements}    void close(){//implements}    void alarm(){//implements}}
Abstract class Summary
    1. The abstract class typically contains one or more abstract methods, which do not provide implementations. The benefit allows for the separation of the definition and implementation of the method.
    2. Classes that contain abstract methods must be declared abstract classes
    3. All subclasses of the abstract class must provide a specific implementation for the superclass. Subclasses that do not implement an abstract method of extracting a class can produce a compilation error unless the subclass is also declared abstract. The meaning of the existence of abstract classes is to be inherited.
    4. Abstract class declares the common properties and behavior of classes in a class hierarchy, because constructors cannot be inherited, so constructors cannot be declared as abstract methods.
    5. Abstract class cannot instantiate an object of an abstraction, but can declare a variable of an abstract type that is used to refer to an object of a subclass. For example, Animal a=null; a=new Dog(); .
    6. The abstract keyword cannot be applied to static,private,final, methods, because these methods cannot be overridden and therefore cannot be implemented in subclasses.
Interface Summary
    1. The interface starts with interface and contains a set of abstract methods that are default to public.
    2. An interface can contain variables, the default static final, which must be given an initial value.
    3. Implementing an interface must implement all of these methods.
    4. If a class does not implement any interface methods, it is an abstract class and must be declared in abstract.
    5. Implementing an interface is equivalent to reaching a protocol with the compiler, "I will implement all the methods that the interface has developed".
Comparison of the advantages and disadvantages of interfaces and abstract classes
    1. A subclass if you implements an interface, you must implement all the methods of the interface (whether or not it is required), and inheriting an abstract class requires only the required method, which is an advantage of the abstract class.
    2. If the method name in an interface changes, then all subclasses implementing the interface will not compile, because the methods they implement are no longer present, which is a disadvantage of the interface, and a non-abstract method in the abstract class changes, there is no such problem, just for the subclass added a new method.
    3. The huge disadvantage of abstract classes is that a subclass can inherit only one superclass.

Resources:

Explanations of abstract class and interface in Java and their similarities and differences (RPM)

Abstract Java keyword

Java Abstract and interface parsing

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.