Summary of Java face questions (II.)

Source: Internet
Author: User

A good interview with a friend---rewrite, reload

Perhaps because object-oriented thinking is important for programming languages, rewriting and overloading always have a place in the interview question. Some interviews may even intentionally include rewriting and overloading in English, for example, briefly describing the difference between overload and override.

Here's how to answer this question from the interview point of view.

Overloading: Describes the same class, the method names of two methods are identical, the parameter list is different, method modifiers, return value types can be the same or not the same, following the syntax of compile-time binding (static binding).

Rewrite: Describes the relationship between the two different non-static methods in two or more classes with an inheritance relationship, the method name of the two methods, the argument list must be identical, access modifiers, return value types, exception types must be compatible , and follow the syntax of runtime binding (dynamic binding).

After the rewrite, you can properly expand the " compatible " ( matching ) The connotation and extension of the word.

Method access modifier, return value type, exception type must match refers to the following three points:

1) Subclass overrides the parent class's method access permission cannot be less than the access permission of the parent class method;

2) If the base data type or void type, the parent, subclass method return value must be the same, if the reference data type, the subclass overrides the parent class's method return value type cannot be greater than the parent class method return value type;

3) Subclasses overriding the parent class's method throws an exception of type range that cannot be greater than the parent class method that throws the exception type.

This is done in order for the parent class to refer to an object that can fully accept the child class when it is being shaped upwards.

Originally I do not want to make such a routine, the interviewer can whether your technology is really a good, you do not answer this basic problem, others think you food or logical thinking ability, language expression ability is not strong, so in the salary treatment, on ... Do you have the nerve to ask for this price? Who care? Go to the society, still have to learn how to deal with the routine, so as to avoid being a routine everywhere.

Deep understanding of overrides, overloading mechanisms

classFather { Public Static voidPrintinfo () {System.out.println ("Fatherclass Info"); }} Public classChildextendsFather { Public Static voidPrintinfo () {System.out.println ("ChildClass Info"); }     Public Static voidMain (string[] args) {Father cc=NewChild ();    Cc.printinfo (); Output Fatherclass Info}}

classFather { Public voidPrintinfo () {System.out.println ("Fatherclass Info"); }} Public classChildextendsFather { Public voidPrintinfo () {System.out.println ("ChildClass Info"); }     Public Static voidMain (string[] args) {Father cc=NewChild ();    Cc.printinfo (); Output ChildClass Info}}

Let's look at a question about the type of return value.

Method overrides require access permissions, throw exception types, and return value type matches.

The following code, the output is? classFather { Public intGetnum () {return1; }}classChildextendsFather { Public LongGetnum () {return2; }} Public classTest { Public Static voidMain (string[] args) {fathe F=NewChild ();    System.out.println (F.getnum ()); }}a,1B,2 C, compile error D,12

If you only have a rough understanding of the return value type relationship between the parent-child class method of the rewrite, you will make a mistake on this topic.

It must be noted that the subclass method return value type is not greater than the reference data type for the parent class method return value type, and must be the same for the base data type and void type.

I want to talk to someone for a day.

If it's difficult to understand my ideas, take a look at one of my other articles about the Java object Model---"Java Model (i)".

This article from the Foreign Programmer exchange Community Programcreek, in my translation to add a lot of their own understanding. Since this is the first blog I wrote after touching Java, many aspects of improper handling, I hope to forgive.

Back to read this blog before, found that they have been more immature, and now indeed grow a lot, many do not understand the content also slowly understand.

Before someone said that I did not mean much to study these things, it was important to understand the breadth, not the depth. In fact, it seems to me that this is just a difference in people's values, outlook on life and even world views. (Attached: I actually write the world view, but some people say that even the whole earth does not look at the complete, where the worldview. So, I still have to delete these words. Everyone has a different pursuit, many people will move with the tide, but I hope that they are the original self, will not for the test and rote, not to beg for life and give up beginner's mind.

Maybe a lot of people ask, why my blog always has the taste of chicken soup. Mainly I feel too lonely, no one can talk, feel no one can understand me, perhaps I do not want to let others understand in real life. Recently, the pressure is particularly large, slowly by the life of the body to smooth the ground. Take one step at a glance.

Summary of Java face questions (II.)

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.