6th. Interfaces in Java and abstract classes

Source: Internet
Author: User
Tags iterable

Interface Interface

In the Java programming language is an abstract type, which is a collection of abstract methods. Interfaces are usually declared in interface. A class implements an abstract method of an interface by implementing an interface (implements) approach.

    • The interface is defined by the interface keyword.
    • The method cannot be implemented in an interface, but it can be declared
    • class implements the interface through the Implements keyword
    • If a class implements an interface, it must implement all the methods in the interface
    • A class can implement multiple interfaces through the Implements keyword, but can inherit only one parent class
    • interface-oriented polymorphism (interface reference to its implementation class)
    • Interface cannot be instantiated
    • Methods in an interface are abstract methods
    • There is generally no attribute in the interface, and there must be declared as Pulic static & final form (does not declare that the compiler is automatically set to public static final type)
The difference between an interface and a class
    • An interface cannot instantiate an object
    • Interface has no construction method
    • All methods in an interface must be abstract methods
    • An interface cannot contain member variables unless it is a public static final variable
    • Interfaces are not inherited by classes, but are implemented by classes
    • The interface supports multiple inheritance (the interface can only inherit extends relationships between interfaces, and multiple inheritance is possible)
The difference between an abstract class and an interface
    • Common methods and abstract methods can coexist in abstract classes
    • There can be various types of member variables in an abstract class
    • Interfaces cannot contain static methods, and abstract classes can have
    • A class can inherit only one abstract class, but it implements multiple interfaces
Declaration of the interface

(Think about it or the Rookie tutorial statement is good to write)

interface Interface Name [extends  other class name        ] {//  declare variable         //  abstract method }
Implementation of the interface

Class must implement all the methods of an interface when implementing an interface.

The interface can implement polymorphic functions.

Example
Interfaceusb{ Public voidconnect ();}//USB Interface for mouse classclassMousesImplementsusb{@Override Public voidConnect () {System.out.println ("Mouse Connect USB"); }}//USB Interface for keyboard classclassKeyBoardImplementsusb{@Override Public voidConnect () {System.out.println ("Keyboard connected to USB"); }}

We can implement different object-to-interface implementations in the following ways:

 Public Static void USEUSB (USB usb) {    usb.connect ();}  Public Static void Main (string[] args) {    new  mouses ();     New KeyBoard ();    Useusb (mouses);    Useusb (KeyBoard);}

The Useusb method is used to pass different USB interface objects to achieve polymorphic effect. The final print results are as follows:

Mouse connect USB keyboard connect usb
Inheritance of interfaces

There is no focus on the teacher's class, but the following chapter is a clear example.

Iterator interface:

 Public Interface      Some ways     to iterable<t> {    /*** iterators * *

......
}

Collection Interface:

 Public Interface extends       Some ways             to iterable<e> {/** * Collection * * ......}

List Interface:

 Public Interface extends      Some methods     of collection<e> {    /*** List */   ...}
Multiple inheritance of interfaces

The inheritance of the interface is a big difference from the inheritance of the class, which is that the interface can inherit more, and the multiple inheritance is similar to the way the class implements multiple interfaces:

 Public Interface extends Sports, Event
Java Abstract Class (interview)

Classes modified with Abstract are called abstract classes.

Some features of abstract classes
    • Abstract classes cannot be instantiated, only subclasses of abstract classes can be instantiated.
    • Abstract classes do not necessarily contain abstract methods, but classes that contain abstract methods must be abstract classes
    • Construction methods cannot be declared as abstract methods
    • Subclasses must have all the abstract methods of the parent abstract class, unless the subclass is also an abstract class
    • Abstract methods are called abstract methods, only methods are declared, and there is no method body
    • A method in an abstract class can be an abstract method, or it can be a common method
    • Attributes cannot be modified by an abstract property

6th. Interfaces in Java and abstract classes

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.