Java object-oriented Summary

Source: Internet
Author: User

1. Object-oriented features:

A) Abstraction: the commonality of things; good at dividing the boundaries of problems.

B) Inheritance: inheritance is a mechanism by which child classes automatically share parent class data and methods.

C) Encapsulation: The methods and attributes of an object are encapsulated together. High Cohesion and low coupling of software components are realized ".

D) polymorphism: the Instance Object of the class to which a referenced variable directs, and the method called by the referenced variable to which class is implemented, the polymorphism can be determined only when the program is running. Implement polymorphism through method rewriting.


2. understanding of key knowledge

1) method overload and override)

Heavy Load)

1. methods with the same name in the same class, but their parameter lists are different.

2. overload cannot be implemented through different return values.

Rewrite

1. A method name and parameter in the subclass are identical to that in the parent class.

2. When a subclass overrides the parent class method, it can only throw fewer exceptions than the parent class ).

3. The access permission of the subclass method can only be greater than that of the parent class.

4. The private method of the parent class cannot be overwritten. If the subclass overrides a method that is identical to the parent class name and parameter list but has the permission to access the private method, the subclass only defines a new method. This method is not actually rewritten.


2) constructor

The constructor cannot be overwritten and can only be reloaded.

Rules:

A) if a class does not define the constructor, the compiler defaults to a non-argument constructor.

B) when the subclass calls the constructor, it always calls the constructor of the base class first.

C) if the constructor is customized, the default constructor will not exist.


3) abstract classes and interfaces

Abstract class Interface

1) constructor

2) There can be common methods and common member variables, and there can be no abstract members.

3) play a role in code implementation to achieve code reuse

1) All methods are public abstract

2) All member variables are public static final type constants)

3) constructor is not allowed.

4) It plays a role in the system architecture and is used to define the communication between modules.

Only one abstract class can be inherited and multiple interfaces can be implemented.

4) Common Object Methods

Clone () Equals) finalize ()

Method used by the thread: Y () notifyAll () wait (long timeout)


When a class inherits a base class, the program code loading sequence is as follows:

1. Static code blocks in the base class are loaded only once .)

2. constructor in the base class and constructor of the specific class

3. The static method must be called before execution.

Example:

Class A {static {// The base class static code segment is first loaded and only loaded once. out. print ("a" + "");} public static void prt1 () {System. out. print ("1" + "");} public A () {System. out. print ("A" + "");}} public class B extends A {static {// The static code segment of A specific class is loaded after the static code of the base class is loaded and only once the System is loaded. out. print ("B" + "");} public static void prt1 () {System. out. print ("2" + "");} public B () {System. out. print ("B" + "");} public static void main (String [] args) {B B B = new B (); B c = new B (); A a = new A (); B. prt1 ();}}

Output: a B a B A 2

This article is from the blog of "Italian suckling pig", please be sure to keep this source http://lovesisi.blog.51cto.com/6414433/1301664

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.