Java FAQ (5)

Source: Internet
Author: User

V. Object-oriented

Q5.1 what is the difference between extends and implements?
A: For a class, extends is used to (single) inherit a class, while implements is used to implement an interface ). The interface is introduced to partially provide the multi-inheritance function. In the interface, you only need to declare the method header and leave the method body to the implemented class. These implemented class instances can be treated as interface instances. You can also declare an extends (Multi-inheritance) Relationship between interfaces. Note that one interface can be extends with multiple other interfaces.

Q5.2 how does Java implement multi-inheritance?

A: Java does not support explicit multi-inheritance. In explicit multi-inheritance languages such as C ++, The subclass is forced to declare the ancestor's virtual base class constructor, which violates the object-oriented encapsulation principle. Java provides the interface and implements keywords to partially implement multi-inheritance. See q5.1.

Q5.3 what is abstract?

A: The method declared as abstract does not need to be given as a method body and is left to the subclass for implementation. If an abstract method exists in a class, the class must also be declared as abstract.
Classes declared as abstract cannot be instantiated, although they can define constructor for subclass.

Q5.4 what are the differences between public, protected, and private?

A: These keywords are used to declare the visibility of classes and members. Public members can be accessed by any class. Protected members are limited to their own and sub-class access, while private members are limited to their own access. Java also provides the fourth default visibility, which is generally called package private. When there is no public, protected, or private modifier, the members are visible within the same package. Class can be modified using public or default.

Q5.5 what is the difference between override and overload?
A: override refers to the inheritance relationship between the parent class and sub-classes. These methods share the same name and parameter type. Overload refers to the relationship between different methods in the same class (which can be defined in subclass or parent class). These methods have the same name and different parameter types.

Q5.6 I inherited a method, but now I want to call the method defined in the parent class.

A: You can use super. XXX () to call the parent class method in the subclass.

Q5.7 What should I do if I want to call the constructor of the parent class in the constructor of the subclass?

A: Call super (...) In the first line of the subclass constructor.

Q5.8 I have defined several constructor methods in the same class and want to call another one in one constructor.

A: call this (...) In the first line of the constructor (...).

Q5.9 what if I didn't define the constructor?

A: automatically obtain a construction method without parameters.

Q5.10 I failed to call the construction method without parameters.

A: If you define at least one constructor, there will be no automatically provided parameter-free constructor. You need to explicitly define a construction method without parameters. Another possibility is that your constructor method or class is not public. For more information, see q5.4.

Q5.11 how can I define a destructor similar to C ++ )?

A: A void finalize () method is provided. This method is called when garbarge collector recycles this object.

Note that it is difficult to determine when an object will be recycled. The author never felt the need to use this method.

Q5.12 What should I do if I want to convert a parent class object into a subclass object?
A: Forced type conversion. For example
Public void meth ()
{
B = (B);
}
If a is not a B instance, classcastexception is thrown. Therefore, make sure that A is indeed a B instance.

Q5.13 I'm not sure if A is a B instance. Can I handle it by situation?
A: You can use the instanceof operator. For example
If (A instanceof B)
{
B = (B);
}
Else
{
...
}

Q5.14 I modified the value of an object in the method, but after exiting the method, I found that the value of this object has not changed!
A: It is very likely that you have assigned a new object to the input parameter. For example, the following code may cause this error: Public void fun1 (a) // A is a local parameter, points to an external object.
{
A = new A (); // A points to a new object and is decoupled from the external object. If you want to make
Outgoing variables,
Do not write this sentence.
A. setattr (ATTR); // modified the value of the new object. The external object is not modified.
}
This also happens to the basic type. For example:
Public void fun2 (int)
{
A = 10; // only applies to this method, and the variables outside will not change.
}

 

 

From: http://www.cndw.com/tech/page/200602154467.asp

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.