Object-oriented Java Basics (2)

Source: Internet
Author: User

Abstract class

1. A class that contains an abstract method is an abstract class that is to be declared with an abstract adornment and cannot be directly instantiated with its object.
PS: What is an abstract method? is a method that is defined only and not implemented (public abstract void Fun ()), which is no {...} Method body ...} The method.
2. Abstract classes are inherited to subclasses, and subclasses of the inherited abstract class must overwrite all abstract methods, otherwise the subclass is an abstract class and cannot instantiate an object.
3. Abstract classes Of course cannot be modified with final
Cases:

/** * 定义抽象类,用abstract修饰 * 至少包含一个抽象方法*/publicabstractclass AbstractClass {    //抽象方法,没有方法体    publicabstractvoidfun();}
Interface

1. The interface is a special class, and the interfaces in Java are composed of abstract methods and global constants.
Cases:

/* * 定义一个接口 * 由全局常量和抽象方法组成 * */publicinterface InterfaceDemo {    publicstatic"CHNA";//全局常量    publicabstractvoidfun();//抽象方法}

2. Interface is for a subclass implementation, with the Implement keyword
Cases:

/* * 实现接口 InterfaceDemo * 要覆写接口的全部抽象方法 * */publicclass ImplementInterfaceDemo implements InterfaceDemo {    @Override    publicvoidfun() {}}

3. When defining an interface, the modifier public static final keyword of the internal global constant can be omitted, and the modifier of the internal abstract method public abstract keyword can be omitted
4. If a class has both inheritance and implementation, it inherits and implements it first.
Cases:

class A extends 类名称 implements 接口名称1,接口名称2{}

5. An abstract class can implement multiple interfaces, but an interface can not inherit abstract classes, you can implement multiple interfaces
The difference between an abstract class and an interface:

Anonymous inner class

An object is used only once, and anonymous objects can be used, and anonymous inner classes can be used if a class is used only once.

 Public InterfaceA Public void  Fun();}//Implement a interfaceClass B implements A {@Override Public void  Fun() {System. out. println ("Hello world!"); }}class xx{ Public void Temp(A A)    {A.fun (); } Public void fun1() {Temp (NewB ()); }} Public classdemo{ Public Static void Main(string[] args) {NewXX (). FUN1 (); }}

If Class B is used only once, the above code can be implemented with an anonymous inner class:

publicclass X {    publicvoidtemp(A a){        a.fun();    }    publicvoidfun1(){        temp(new A(){            @Override            publicvoidfun() {                System.out.println("hello");            }        });    }}
Abnormal

Exception handling mechanism in 1.Java: Catch exception, throw exception
2. Exception handling can allow the program to continue execution if there is an error
3. Exception Handling Format:

try{    可能出现异常的代码段}catch(异常类 异常对象){    处理异常}catch(异常类 异常对象){    处理异常}....finally{    异常的出口,不管是否有异常,该段语句都会执行}

Package and access rights
1. Package is a folder that uses the Package keyword to declare packages that need to be in the first row of a class file
2. Use other packages in the program, you can re-import the specific location, using the import statement to express
3. Static import of packages, if the methods or variables in a class are all static, you can use static import, rule: import static package name
4. Access permissions for the package:

Not to be continued ....

Object-oriented Java Basics (2)

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.