Abstract classes and Interfaces

Source: Internet
Author: User
Tags modifiers


1. Abstract class

Rules about Abstract class:

(1) A abstract class can never be instantiated.

(2) The methods marked abstract end in a semicolon rather than curly.

(3) You can, however, put nonabstract methods into an abstract class.

(4) It is legal this abstract class has no abstract methods.


Example:

1) Several rules applied

Abstract class Animal {//default, package access protected int age; private string name = "Animal";//yes, abstract class can has private fields private string GetName () {//yes, Abst
    Ract class can has private Non-abstract method return name; abstract void Eat (String food);//abstact method ends in a semicolon, default acess// Never, ever, ever mark methods as both private and abstract public void setage (int age) {//abstract class can has n             On-abstract methods this.age = age;
                                    OK, use this keywords}//public static abstract void info (); Illegal combination of modifiers:abstract and static public static void info () {//ok, has static non-abstract
    Class System.out.println ("Oh, Animal"); The abstract class Dog {}//ok, nothing abstract class Cat {//ok, abstract class has no abstract methods PuBlic void name () {System.out.println ("Oh, Cat");
    public void Age () {System.out.println (' Oh, my age is secret! '); }
}

(2) About constructor

Abstract class Bird {
    int age;
Public  abstract Bird (int age);  Constructor can not is abstract public 
    Bird (int age) {          //ok
        this.age = age;
    }
}

2. Interface (interface)


Rules about Interface:

(1) All interface methods are implicitly public and abstract. In the other words, I/need to actually type thepublic orabstract modifiers int the method declaration, but the is still alwayspublic andabstract.

(2) All variables defined in a interface must ispublic, static, and final--in other words, inte Rfaces can declare only constants and not instance variables. And in the other words, the any fiels in a interface are automatically public , static, and final.

(3) Interface methods must is notstatic. Because interface methods are abstract, and combination abstract and static is illegal.

(4) Because interface methods areabstract, they cannot be marked final, strictfp, ornative .

(5) An interface canextend one or more other interfaces.

(6) An interface cannotextend anything but another.

(7) An interface cannotimplement another interface or class (of course abstract class).

(8) Interface types can be used polymorphically.


3. Extends. Implements.


(1) Class extends class (at most extends one class)

(2) class implements Interface1, Interface2, ... (Can implements multiple)

(3) Class extends class (at most one) implements Interface1, Interface2, ...

(4) interface extends Interface1, Interface2, ... (Can extendss multiple)

(5) interface can not extends class



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.