Java face question 002

Source: Internet
Author: User

1, the following code to determine the operation of the situation

 Public classTestextends { Public Static voidMain (string[] args) {Father obj=NewSon ();    System.out.println (Obj.getele ()); }}classfather{ Public intGetele () {return1; }}classSonextendsfather{ Public floatGetele () {return2f; }}

Answer: compilation error. The reason is that a function cannot be distinguished by a return value, although the parent class has a different return value than the function in the subclass, but they have the same function name, so the compiler cannot distinguish it.

2, abstract method permission ID can only be modified by public or protected "NOTE: You can not do permission decoration-default".

3, the method in the interface can only be modified by public,abstract.

4. What are the internal classes and what are their characteristics?

The inner class is divided into four types: static inner class, member inner class, local inner class, and anonymous inner class.

① static inner classes are statically decorated, can be instantiated without relying on external instances, cannot have the same name as external classes, and cannot access non-static members of external classes.

② member inner classes are free to reference external member variables and methods (including private)

③ local inner classes cannot be modified by permissions modifiers such as public and static

④ Anonymous inner classes cannot use class, extends, implements, no constructors, must inherit classes, or implement interfaces, thus creating only one anonymous inner class instance. In addition, local inner class constraints also apply to anonymous inner classes.

5, how to get the class name of the parent class?

Answer: Use GetClass (). Getsuperclass (). GetName ().

Note : Why you cannot use Super.getclass (). GetName (), as explained below:

The GetClass () method is defined in the object class as final and native, which cannot be overridden by subclasses. So This.getclass () and Super.getclass () all call the object's GetClass (), and the interpretation of object's GetClass is a running class that returns object.

6, why should have super keyword?

In the example of a subclass that integrates the parent class, if the subclass overrides the method of the parent class, it must be called with super if you want to invoke the original method of the parent class.

7. Examples of super error use:

class father{    public  Father () {        System.out.println ("Dad in this");}    } class extends father{    public  Son () {        System.out.println ("son here ~");         Super ();    }}

Explanation: A compilation error will occur and super () must be the first statement in the subclass constructor.

8.

Java face question 002

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.