Java Basics-Object oriented

Source: Internet
Author: User

Object-Oriented Programming:

1. Basic Features: Abstraction, encapsulation, inheritance, polymorphism.

2. Class and member access control: private: In the same class; default: In the same package; protected: Subclass; public: all;

3. Inherit--[access control modifier] Class < subclass name > extends < parent class name > {subclass}

Rule: 1) Multiple inheritance is not supported.

2) inherit only non-private member variables and methods.

3) The subclass overrides the parent class function with the same name.

       4) The subclass to the parent class can be "transformed upward" to achieve polymorphism.

  4. Super Keyword: You can access the parent class members that are hidden by the Quilt class member, or you can call the parent class's constructor method.

Construct method call rule: 1) call from top to bottom by inheritance relationship.

2) If the subclass does not have a constructor method, the parent class's parameterless constructor is called by default. An error occurs if the parent class does not have an argument-free construction method.

             3) If a subclass has a constructor method, the constructor of the parent class is executed first, and the child class is constructed by the constructor method.

4) If you use super to call the construction method with parameters, you must write in the first sentence.

5. Polymorphic: A subclass object can be used instead of the parent object, that is, the object of the parent class is declared, and then instantiated with a subclass, you can access the subclass's method with the same name (excluding the attributes of the child class).

Mainly divided into methods of overloading and covering:

      - Method overloaded Rules: Different number of parameters, different types of parameters, or different order of parameters, regardless of the return value. (occurs in the same class)

--Method Override rules: Subclasses redefine the parent class method, but the method name, return value and parameter pattern are exactly the same, and access permissions cannot be reduced. (occurs in inheritance)

  6. Object styling: Objects are declared with a parent class, and new join member access is restricted for subclasses, and access to members of the child class can be restored using the coercion type conversion method.

Condition: 1) The type conversion of the subclass to the parent class can be done implicitly.

2) in polymorphic situations, transformations from the parent class to the subclass must be shaped.

3) Conversions between reference types that do not have an inheritance relationship are illegal.

7. Instance of: Used to detect the true type of an object, including its subclasses, the parent class.

8. Covariant return type: That is, allows subclasses to override the return value of the method when overridden, but must be a subclass or an implementation class that returns a value type before overriding.

9. Method overloading and multiple matching: it is determined by the conversion overhead and generally less expensive to convert in a more complex direction. (such as short can match a byte, or it can match int, at which time the int is preferred)

Advanced class Attributes: Abstract classes, interfaces, nested classes, and enumeration types:

1. Abstract class: [Access control modifier] Abstract class < class name > {

member variables;

General method () {}

abstract method ();

}

Rules: 1) can include a specific method, or it can include an abstract method.

2) Abstract methods can only be written within abstract classes.

3) An abstract class can be constructed by means of a constructor, but without an instance, it can only be instantiated by subclasses.

4) For a class, you cannot use both final and abstract to declare, and for member methods, you cannot use both static and abstract declarations.

2. Interface: [access control modifier] Interface < interface name > [Extends < parent interface List;] {

constant

abstract method ();

}

Rule: 1) Multiple parent interfaces can be inherited.

2) The default all member variables are public,static and final.

3) All member methods are public and abstract by default.

4) The interface includes only constant definitions and abstract methods.

5) The access rights of the interface are public and default only.

Features: Interfaces can be multiple implementations, or multiple inheritance.

Differences from abstract classes: 1) Multiple inheritance.

2) Abstract classes can have member methods that contain concrete implementations, while interfaces do not.

3) The interface is not part of the class system.

4) has a pluggable nature.

if: (1) The parent class already has an implementation of the same name method.

(2) This class is an abstract class.

At this point, you do not need to implement all the methods in the interface.

3. Nested classes: Similar to member methods, members of other outer classes can be accessed in nested classes, including private members.

public class Testinners {public    static void Main (string[] args) {        A A = new A ();        A.B B = a.new B (); B is an inner class that requires an object of a to instantiate        b.mb ();}     }

Anonymous inner class:

public class TestAnnoymous2 {public    static void Main (string[] args) {        TestAnonymous2 ta = new TestAnonymous2 (); 
   ta.test (New Swimmer () {///using an anonymous inner class, implements the swim method of the Swimmer interface public            void swim () {                System.out.println ("I ' m Swimming! ");}}        );        public void Test (Swimmer Swimmer) {        swimmer.swim ();}    } Interface Swimmer {public    abstract void Swim ();}

4. Generics: [Access control modifier] class name < parameter type list > [extends] [implements] {}--implements type parameterization, avoids coercion of type conversions. (Details will be expanded later)

5. Exception handling: All exception classes implement the Throwable interface. Divided into two types of error and exception. Error refers to internal errors in the JVM system and resource exhaustion (critical). Exception is also divided into compile-time exceptions and run-time exceptions, which are usually caused by an external factor that becomes wrong or accidental. (Details will be expanded later)

Exception examples (not all):

Linkageerror: the. class file does not exist at the time of connection (moved or deleted).

Virtualmachineerror: Application recursion too deep, stack memory overflow (space exhausted).

NullPointerException: null pointer exception.

Java Basics-Object oriented

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.