Dark Horse programmer--java Base-Object oriented

Source: Internet
Author: User

-------Android Training, Java training, look forward to communicating with you! ----------

Object-Oriented Overview:

Elephant to the refrigerator for example. When we talk about object-oriented, we must talk about the process.

Process oriented: Open the refrigerator, load the elephant, and close the refrigerator. These three steps are the process-oriented way of thinking, which emphasizes the process and can also be called action. That's the way it is in C.

Object-oriented: refrigerator open, refrigerator storage, refrigerator off. This is the object-oriented way of thinking, which emphasizes the object, or can be said to be an example. Used in java,c++,c#.

Object-oriented features

1. Object-oriented thought conforms to people's habitual thinking way.

2, the appearance of object-oriented, to simplify the problem

3. Object-oriented will be the executor of the process, become the object of the conductor.

Class and object relationships:

Simply put:

Class: The description of things, the description of things usually have attributes and behavior two aspects, as long as they are clear and defined in a class can be

Object: an instance of such a thing, which is the entity of the real existence of such things.

Objects are created by using the keyword new.

use conditions for anonymous objects :

Condition One: When an object method is called only once, it can be done with an anonymous object, which makes it easier.

Condition Two: An anonymous object can be passed as an actual parameter.

How the object is called in memory:

Creating an object from new creates an object in heap memory that contains an address pointer and a default value initialized by the constructor. The stack memory is called by the object pointing to the address pointer of the heap memory.

The difference between a member variable and a local variable in a class:

1.

Member variables are defined in the class, and the entire class can be accessed.

Local variables are defined in functions, statements, and local code blocks, and are only valid for access in the area to which they belong.

2.

The member variable exists in the heap memory object.

Local variables exist in the stack memory method.

3.

The member variable exists as the object is created and disappears as the object disappears.

Local variables exist with the execution of the owning region and are released as the end of the owning region.

4.

Member variables have default initialization values.

Local variables do not have default initialization values.

Characteristics of Class-type variables:

The class type variable must point to the object, otherwise it is null.

Object-oriented three major features:

Three characteristics of object-oriented: encapsulation inheritance polymorphism

1. Encapsulation: Refers to the properties of hidden objects and implementation details, only provide public access to the outside.

Benefits:

1. Isolate the changes.

2. Easy to use

3. Provide code reuse rate

4. Improve security

Principles of Encapsulation:

1. Hide content that does not need to be provided externally.

2. Hide attributes, provide public access to access them

2. Inheritance: 1. Improve the reusability of code; 2. There is a relationship between classes and classes, and with this relationship, there is a polymorphic feature.

Note: Do not inherit by simplifying code to get the functionality of other classes, it must be a relationship between classes and classes to inherit.

In Java:

Java only supports single inheritance and does not support multiple inheritance.

Because multiple inheritance is a security risk: when the same functionality is defined in more than one parent class, the subclass object is not sure which one to run when the feature content is not the same. But Java retains this mechanism and uses a different form of representation to do it: multiple implementations

Java supports multiple layers of inheritance, which is an inheritance system. But how do you use the functionality of an inheritance system?

To use the system, first consult the description of the parent class in the system. Because the parent class is defined in the system of the common features, by understanding the common features, you can know the basic functions of the system, then the system can be used basically.

So when you make a specific call, you create the object of the most child class, why?

One is because it is possible that the parent class cannot create the object, and the most subclasses implement the most functionality.

Inherits the characteristics of class members in a neutron parent class.

Class Members:

Variable

1. When a non-private variable with the same name appears in the subclass, the subclass accesses the variable in this class with this

2. Subclass to access variables of the same name in the parent class, use super

Function

1. When a child class appears with the same function as the parent class, when the subclass object calls the function, the contents of the subclass function are run, as if the parent class's function is overwritten, which is a feature of the function: override (overwrite).

2. When the subclass inherits the parent class, inheriting the function of the parent class into the subclass, but the subclass has the function, but the content of the function is inconsistent with the parent class, there is no need to define the new function, and the other is to use the overriding method, preserve the function definition of the parent class, and rewrite the function content.

Coverage Details:

Subclasses override the parent class, you must guarantee that the child class permission is greater than or equal to the parent class permission before overriding the

Static can only override static, non-static can overwrite

Overloading: Class names and methods are the same

overriding: Child parent class methods are identical

constructor function:

When the child class object is initialized, the constructor of the parent class is also run, because the constructor of the subclass defaults to the first line with an implicit statement super ();

Super (); The constructor that accesses the null argument of the parent class, and all constructors in the subclass default to the first row are super ();

Why must subclasses access constructors in the parent class?
The data in the parent class is directly fetched, so when the subclass object is established, you need to see how the parent class initializes the data. So when the subclass initializes the object, it accesses the constructor in the parent class first. If you want to access the constructor specified in the parent class, you can define the super statement by specifying the
NOTE: The super statement must be defined in the first row of the subclass constructor.
The instantiation process of the subclass.

1. All constructors in a subclass will default to the constructor of the null argument in the parent class, since the first row in each sub-class construct has the default statement super (); 2, if there is no constructor for the null argument in the parent class, within the constructor of the child class, Constructors in the parent class to be accessed must be specified through the Super statement, and the constructor in the parent class is accessed by the Kawai class constructor using this to specify the calling subclass's own constructor.

3. Polymorphism: A variety of forms of the existence of things.

Polymorphism is also a very important part of Java, is a method name can be used many times, depending on the parameters of the different to distinguish you call which method, the popular explanation of more than one, if there is a method named: "Animal Cry"
Then, when you pass the parameter is animal dog, then call the dog cry, is the cat, then call the cat cry, this is polymorphic.

There are two kinds of polymorphism: universal polymorphism and specific polymorphism. The difference is that the former does not restrict the type of work, allows the same code to be executed on different types of values, the latter is valid only for a limited number of types, and may have different code to perform for different types of values.
The generalized polymorphism is divided into the parameter polymorphism (parametric) and the inclusion polymorphism (inclusion), the specific polymorphism is divided into overload polymorphism (overloading) and forced polymorphism (coercion).

Class test{  void print ()  {   System.out.println ("Hello Test");  }  public static void Main (String []args)  {     a a=new a ();    A.print ();  }} Class A extends test{  void print ()  {    System.out.println ("Hello A");}  }

The above is a dynamic polymorphism.

Class test{  void print ()  {    System.out.println ("Hello World");  }  void print (int x)  {    System.out.println ("Hello World" +i);  }  public static void Main (String []args) {   Test ts=new test ();   Ts.print ();   Ts.print (10); }}

Static polymorphism.

A a=new b (); if (a instanceof B) System.out.println ("a instanceof B");

In simple terms, this is a polymorphic example!

Dark Horse programmer--java Base-Object oriented

Related Article

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.