Java Interview Third Day

Source: Internet
Author: User

Classes and objects:

Class: Subjective abstraction, which is the template of an object that can instantiate an object----a collection of objects that have the same properties and behavior.

The customary class defines the format:

Package xxx;

import xxx;

public class xxxx{

Attributes ...

Constructors ...;

Methods ...

}

Distinguish between instance variables and class variables, both have initial values, or are initialized at the time of definition or initialized in constructors, and local variables have no initial values and must be initialized at the time of definition.

Defining attributes: Instance variables

Format: [modifier] type variable name [=?]

The instance variable is defined in the class but outside of any method.

Instance variables have default values: a variety of 0. (same array)

The scope of the instance variable is at least within this class and is restricted by the access control character.

In coincident scopes, instance variables and local variables allow naming conflicts, "local precedence".

Define the method:

Format: [modifier] Return type method name (argument list) [throws exception] {...}

All parameters in Java are value passing.

When no value is returned, the return type must be defined as void.

The return type must be adjacent to the method name, and other modifiers can be swapped for positions.

Constructor:

The method that is called during the creation of the object.

The constructor does not have a return type.

The name of the constructor is the same as the class name.

The format is: [Modifier] class name (argument list) {}, modifier can be private, protected, default, private

The constructor is used only once in the build cycle of an object and is automatically called by the system and is not allowed to be called manually.

The programmer does not provide a constructor, and the system automatically provides an argument-free constructor.

How to get objects:

With new (requesting allocation of space in the heap), the new class name () can be used in this form or an object, where the object is unusable and the address must be stored in an object variable.

For example:

Car c=new car ();

Attention:

It is best to provide a parameterless constructor when writing the class.

The default is to bring a parameterless constructor, but if you manually write a constructor with parameters, you must add an parameterless constructor.

This keyword:

This is an implicit parameter that represents the current object;

Publie class student{

private String name;

public void SetName (String name) {

This.name=name; THIS.name is a member variable of the current object

}

}

If the first statement of a constructed method has the form this (...), then this constructor invokes the other constructor methods in the same class.

Attention:

The This (...) in the constructor Must be placed on the first row of the constructor.

This cannot appear in a static method

The relationships of classes, objects, and instances:

Class: is a template for an object that can instantiate an object

Object: The individual of the class

Instance: the object being implemented

Student S;

S=new student ();

Where Student is a class, S is an object, new Student () is an instance, and S is assigned as an instance.

Method overloads:

The method name is the same, the parameter table is different, regardless of the return value type (but it is best to make the return type consistent).

The compiler chooses a method based on the parameters, and if there is no exact match, the "up nearest matching principle" is used for the parameter table, but ambiguity is not allowed.

Method overloading masks The difference in the same class of methods of an object due to different parameters.

Packaging:

class, and the private modifier, to restrict access to the inside of the class, and to effectively protect the data.

For a private property in a class, to give it a pair of methods getxxx (), setxxx () accesses the private property, guaranteeing the security of the operation of the private property.

method exposes the declaration of a method, that is, the method can be called only if the parameter and the return value are known, and the details of the implementation of the method are hidden.

The connection between an object and the outside world should be exposed through a unified interface that should be exposed and hidden.

Inherited:

A parent class to a subclass is a general to a special relationship.

Generalization: The process of abstracting commonalities from different subclasses into a parent class.

Special: In the original parent class based on the process of adding some personality.

Principle: The parent class puts the generality, the subclass puts the individuality.

Inherited keyword: extends

Java only supports single inheritance: A class has a maximum of one direct parent class.

Method overrides:

Method Name: Same

Parameter table: same

Access restriction: same or wider

Return value type: Same or subclass return type is a subclass of the type returned by the parent class (after JDK5.0)

Thrown exception: cannot be wider than the parent class.

Super Keyword:

Super () represents the constructor that invokes the parent class

Super () is also the same as this () must be placed in the first sentence of the method

Super () and this () cannot appear simultaneously

Super can mask the properties of the subclass attribute and the parent class when the name of the property is masked, super. Represents a method or property that invokes a parent class

In the constructor of a subclass, if you do not specify which constructor to call the parent class, then the parent class's parameterless constructor is called, Super ()

Attention:

The constructor of the parent class cannot inherit from the quilt class

Methods and properties can be inherited, and permissions are not limited to whether or not they are inherited, limiting the ability to directly access

That is, private inheritance, although subclasses are not directly accessible, but subclasses still have them.

First constructs the parent class, after constructs the subclass, first this after super

Polymorphic:

There are two kinds of polymorphism: compile-time polymorphism and run-time polymorphism.

Compile-time Type: Subjective concept, think of it as what.

Runtime type: Objective concept, actual what it is.

Example: Animal a=new Dog ();

Pointing to the dog asked, what is this animal?

Three principles of run-time polymorphism:

The object type does not change.

methods defined in compile-time types can only be called on objects.

When the program runs, it is called after the overridden method, depending on the object's run-time type. (Run-time dynamic type binding)

Coercion type conversion: there must be no new object generation. (a reference to a parent class that is assigned to a subclass requires a forced type conversion)

Keyword: instanceof

Usage: Refer to the Instanceof class name to determine whether the object pointed to by this reference belongs to this class.

Avoid type conversion exceptions before casting.

if (a instanceof Dog) {

Dog d= (dog) A;

}

polymorphic role: The different sub-class objects are considered as the parent class, you can block the differences between the different sub-class objects, write generic code, make general programming to adapt to the changing needs.

Java Interview Third Day

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.