Abstract classes and interfaces for the Java Foundation

Source: Internet
Author: User

abstract classes and InterfacesI. Abstract classes and abstract methodsFor abstract classes, you first need to introduce abstract methods, and abstract classes exist because of the existence of abstract methods 1. Abstract method (1) Necessity:

  Some parent methods are not sure how to write the method body, but this method must be used for subclasses that inherit it. need to use abstract methods

For example: For animals, we know that the parent "animal" will be called, but because different animals call different methods, and for the subclass "dog" This subclass must use the "called" method. So the "call" method of the parent "animal" does not know how to write. The abstract method is used here.

code example:

1 //Abstract class explanation2  Public classTest abstract class {3      Public Static voidMain (string[] args) {4Dog1 dog =NewDog1 ();5 dog.cry ();6     }7 }8 9 Abstract classanimal{Ten String namestring; One     intAge ; A     //animals will bark, but don't know how to call it, set it up as an abstract method . -     Abstract  Public voidcry (); - } the  - classDog1extendsanimal{ -     //because the "called" Method in the parent class is not implemented, you must override the -      Public voidcry () { +System.out.println ("Barking Dog")); -     } +}
(2) format and specification:

1) must be modified with an abstract, such as the abstract public void Cry ();

2) abstract methods can not have method body , that is, there is no {} to store the method body, because we do not know how to implement the method, just know that the subclass will definitely use;

2. Abstract class

  If there is one or more abstract methods in a class, the class must be declared as an abstract class, such as an abstract class for the animal class in code;

Of course, even if there are no abstract methods in the class, we can define animal as abstract classes (uncommon) by using the abstract keyword.

3. Key memory

1) The abstract method has no method body, but defines the method itself;

2) If there is one or more abstract methods in a class, the class must be declared as an abstract class;

3) subclass Inherits parent class, must implement parent class abstract method (override). The reason is that the parent class does not implement the abstract method;

Ii. interface 1. Interface concept and Meaning

(1) Concept: give some methods without content (abstract method), encapsulated together;

Abstract classes that can be understood as abstract (abstract classes can have abstract or non-abstract methods, and all methods in an interface are abstract)

(2) meaning: A subclass can inherit only one parent class, but it may inherit multiple interfaces;

(4) Interface definition:

interface Interface Name [extends  parent interface list]{  [public] [static] [final  ] constant;  [Public] [abstract] method;}

(3) Implement the interface method:

 Class < category name > [extends parent class name] [implements  interface list]{}
2. Key memory

(1) When a class implements an interface, it is required that the class implement all the methods of this interface (the interface is just a definition method, there is no method body, need to implement)

(2) An interface is a completely abstract class, and all methods in an interface cannot have a principal;
(3) The method name, parameter list, and return type can be determined in the interface class, but there is no method body.
(4) The method that implements the interface must be public access rights.

(5) The variables in the interface are static,final by default; The method defaults to public; in development, frequently used variables are placed in the interface;

(6) Interfaces cannot inherit other classes, but can inherit other interfaces;

3. Summary

(1) Use abstract class or interface: If the motive is to implement polymorphism with the interface, if it is to implement code reuse, abstract class;

(2) The interface is a more abstract abstract class , the method in the abstract class can have the method body, all the methods in the interface have no method body. The interface embodies the design idea of multi-state and high-cohesion low-coupling in programming.

III. abstract class and interface comparison 1. Grammatical differences

1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;

2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

2. Differences at the design level

1) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class. for a simple example, airplanes and birds are different kinds of things, but they all have a common denominator, that is, they can fly. Then in the design, the aircraft can be designed as a class airplane, the bird is designed as a class bird, but not the characteristics of the flight is also designed as a class, so it is only a behavioral characteristics, not a kind of abstract description of things. At this point the flight can be designed as an interface fly, including the method fly (), and then airplane and bird respectively according to their own needs to implement the fly this interface. As can be seen from here, inheritance is a "is not" relationship, and the interface implementation is "there is no" relationship. if a class inherits an abstract class, then the subclass must be the kind of abstract class, and the implementation of the interface is there is no, with no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can realize this interface, not flying will not implement this interface.

2) The design level is different, abstract class as the parent class of many subclasses, it is a kind of template design. And the interface is a kind of behavior specification, it is a kind of radiant design. that is, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface has been changed, all implementations of this interface must be modified by the corresponding class.

The most widely circulated example of the Internet: the door and alarm examples: Door's open (), close (), and alarm () are essentially two different categories of behavior, open () and close () belong to the intrinsic behavior of the gate itself, and alarm () is an extension of the additional behavior. So the best solution is to individually design the alarm as an interface that contains the alarm () behavior, door is designed as a separate abstract class that contains open and close two behaviors. Then design an alarm gate to inherit the door class and implement the alarm interface.

Abstract classes and interfaces for the Java Foundation

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.