Basic Java knowledge-object-oriented (2)

Source: Internet
Author: User

Basic Java knowledge-object-oriented (2)
Abstract class

1. The class that contains an abstract method is an abstract class. abstract classes must be modified in abstract form. objects can be declared without instantiating them.
Ps: What is an abstract method? Is a method that is defined only, but not implemented (public abstract void fun ();), that is, there is no {.... Method body ...} .
2. the abstract class is inherited from the subclass. The subclass that inherits the abstract class must overwrite all abstract methods. Otherwise, the subclass is still an abstract class and the object cannot be instantiated.
3. Of course, the abstract class cannot be modified using final.
Example:

/*** Define an abstract class and use abstract to modify * at least one abstract Method */public abstract class AbstractClass {// abstract method. No public abstract void fun ();}
Interface

1. An interface is a special class. interfaces in Java are composed of Abstract methods and global constants.
Example:

/** Define an interface * composed of global constants and abstract METHODS **/public interface InterfaceDemo {public static final String INFO = CHNA; // global constant public abstract void fun (); // abstract method}

2. The interface is implemented by a subclass and the implement keyword is used.
Example:

/** Implement interface InterfaceDemo * All the abstract methods of the interface to be overwritten **/public class ImplementInterfaceDemo implements InterfaceDemo {@ Override public void fun (){}}

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

Class A extends class name implements Interface Name 1, interface name 2 {}

5. An abstract class can implement multiple interfaces, but an interface cannot inherit the abstract class and can implement multiple interfaces.
Differences between abstract classes and interfaces:

Anonymous internal class

An object is used only once and can use anonymous objects. If a class is used only once, an anonymous internal class can be used.

Public interface A {public void fun () ;}// implement class B implements A {@ Override public void fun () {System. out. println (Hello World !); } Class xx {public void temp (A a) {. fun () ;}public void fun1 () {temp (new B () ;}} public class Demo {public static void main (String [] args) {new xx (). fun1 ();}}

If Class B is used only once, you can use the anonymous internal class to implement the above Code:

public class X {    public void temp(A a){        a.fun();    }    public void fun1(){        temp(new A(){            @Override            public void fun() {                System.out.println(hello);            }        });    }}
Exception

1. Exception Handling Mechanism in Java: catch exceptions and throw exceptions
2. Exception Handling allows the program to continue executing when an error occurs.
3. Exception Handling Format:

Try {code segment with exceptions} catch (exception-type exception object) {Exception Handling }.... finally {exit of the exception, whether or not there is an exception, this statement will be executed}

Package and access permission
1. A package is a folder. To declare a package using the package keyword, you must first line in a class file.
2. Use other packages in the program. You can import the packages at different locations and use the import Statement
3. static package import. If all methods or variables in a class are static, you can use static import. Rule: import static package name
4. Package access permissions:

 

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.