[Java] Abstract classes and interfaces

Source: Internet
Author: User

Directory
    1. Abstract class
    2. Interface
1 Abstract class Definitions

If a class does not have enough information to describe a specific object and needs other concrete classes to instantiate the class, we call it an abstract class.

Characteristics of abstract Classes
    • An abstract class cannot be instantiated, but it can refer to a child class object.
    • Subclasses must implement abstract methods of the abstract class. If the subclass is also an abstract class, you can not implement methods of the abstract class.
    • An abstract class can contain specific methods, or it can contain abstract methods.
    • The abstract method in the subclass cannot have the same name as the abstract method of the parent class.
    • Abstract cannot decorate the same class with final.
    • Abstract cannot be decorated with private, static, final, or native side by side.
Example
 Public Abstract class test{    //  attribute public    int var = 0;         // abstract Methods     Public Abstract void hello ();         // Specific Methods     Public void hello2{        System.out.println ("Hello2");    }    }

2 Interface Definitions
    1. Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.
    2. Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.
    3. The interface cannot be instantiated, but it can be implemented. An abstract class can inherit an interface but not implement a class in an interface.
    4. An interface type can be used to declare a variable. As a reference.
    5. The interface describes the local part of a specific object.
Interface Features
    • Each method in the interface is implicitly abstract, and the methods in the interface are implicitly designated as public abstract (only public abstract, and other modifiers will be error-marked).
    • The interface can contain variables, but the variables in the interface are implicitly designated as public static final variables (and can only be public, and will be error-marked with private decoration)
    • The methods in an interface cannot be implemented in an interface, but only by the classes that implement the interface.
Example
 Public Interface test{    //  Property public    staticfinalint var = 1 ;     // Method     Public void // implicitly defined as public abstract}

The difference between an abstract class and an interface
    1. A method in an abstract class can have a method body, which is a specific function that can implement a method, but the method in an interface is not.
    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. An interface cannot contain static code blocks and static methods, whereas abstract classes can have static code blocks and static methods.
    4. A class can inherit only one abstract class, and a class may implement multiple interfaces.

Reference:

Http://www.runoob.com/java/java-interfaces.html

[Java] Abstract classes and interfaces

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.