General Java Questions - V

General Java Questions - V

Q:"Also, a subclass cannot override methods that are declared static in the superclass. In otherwords, a subclass cannot override a class method. A subclass can HIDE a static method in thesuperclass by declaring a static method in the subclass with the same signature as the staticmethod in the superclass. "My question: It looks like play with words: override, hide.. Why is it written HIDE?If I can write a static method in subclass with the same signature it means that I override thatmethod in superclass, I really changed that method here in subclass. Am I wrong?Answer: Let's start from the very beginning - definition for overriding. It includes three mainpoints that overridden method must:* have the same name* have the same signature* have the same data type.In your case the static method in subclass can have another data type.Example:************ Alex.java *************package alex;public class Alex {file:///C|/330_new/330_new/general_java-V.htm (1 of 6) [2003-07-22 22:07:56]General Java Questions - Vstatic String al(String name){return name;}}******************** EOF *********************** John.java *************package alex;public class John extends Alex {static int al(int name){return name;}}******************** EOF ***********It compiles! You see that in Alex class the method "al" returns String and in John class - int.It is hiding, not overriding.Advice: try to avoid such situation when you hide methods in super class. Use different namesinstead and you will save a lot of time.--AP (JA)Q: Why C++ is not platform independent?Answer: C++ compiles to binary code (.obj, .exe, .dll, a.out etc.). Binary code (Machine code,0's and 1's) are machine dependent.Java compiles to byte code which is independent of any machine. It need to be interpreted tobinary code by JVM, and executed by the machineQuestion: I know that void method does not return any value. But I still write the code likethis:void nothing() {};void nothing2() {return;};Why can we still use "return" in the body of method?Answer: To be able to exit method when it is necessary.You can exit the method when you reach the end of method and do not need to use "return".If due to some condition program must exit the method then you use "return".Q: I can't find the API documentation on any classes in the sun.* packages. Where is it?Answer: The short answer is that SUN provides documentation only for the public classes injava.*. SUN does not provide documentation for sun.* because those are the Sun-specificimplementation, and specifically not part of the Java technology API standard, and aretherefore subject to change without notice.file:///C|/330_new/330_new/general_java-V.htm (2 of 6) [2003-07-22 22:07:56]General Java Questions - VIn general, SUN doesn’t provide javadoc documentation for sun.* classes in order todiscourage developers from writing programs that use them. For further explanation, see thenext question.However, if you must have it, the documentation for sun.* is available separately, here:http://java.sun.com/communitysource/index.html For example, the doc comments for sun.netare in the source files located at:/src/share/sun/sun/net/*.javaThis source code release does not include javadoc-generated documentation. You would haveto generate those docs yourself using javadoc.source: http://java.sun.com/products/jdk/faq.html#A12Q: How do I copy one array to another?Given that I have an byte array defined like this:byte byteSmall = new byte[23];and another larger byte array defined like this:byte byteBig = new byte[30];How do I copy byteSmall into byteBig starting at index 7 without a for loop like this:for(int i = 0; i <> int[] a, b;These are not equivalent, because b is an array only on the right side.Whereas with C-style pointers these would become:int *a, b; int* a, b;which breaks the concept of "white space doesn't matter", so it will not be as easy to scan andparse.So the designers of C decided to have only 1 form of defining variables with pointer types orarray types, and this was that the * or the [] binds to the variable, not to the type.file:///C|/330_new/330_new/general_java-V.htm (4 of 6) [2003-07-22 22:07:56]General Java Questions - VJava carried over the variable binding, but also introduced the type binding, because therewere no more pointers, so the ambiguity was removed.--Joona PalasteAnd think also about marketability!Making Java's syntax highly similar to C and C++'s during its early years:* Made the language seem more familiar to C/C++ programmers.* Decreased the learning curve for C/C++ programmers.* Gained credibility through the frequent inference that it was a next-generation descendant ofC++.* Increased its pool of programmers by attracting C/C++ programmers through the effectsabove.This isn't the only instance of anachronistic syntax; consider the optional semicolon at the endof a class declaration.--Andrew R.Q: When do I need to use overloading in a program? And why?Answer: Overloading is best used when you want to have a function that has the same namebut can accept different arguments. The only restriction is that it must return the same type.e.g.public String aName(String str) {String retStr;retStr = "Hello" + str;return retStr;}// Now the same function but overloadedpublic String aName(String str, String str2){String retStr;retStr = "Hello " + str + str2;return retStr;}Both functions return the same type (a String) but have different signatures.You will find this used a lot with constructors. See any recommended text on Java for a muchbetter explanation.--Anthony Millerfile:///C|/330_new/330_new/general_java-V.htm (5 of 6) [2003-07-22 22:07:56]General Java Questions - VQ: Why people claim that java is a secure language...Answer: The java programming language itself is just as secure or insecure as any otherprogramming language. A Java Applet (which are used on the web) is a different matter. Theyare quite secure.An applet does not have access to system resources outside of the directory that it is createdin. There are ways that you can give an applet access to this information, but they are prettyexplicit and you will most likely know that the applet is trying to do this.I am not really familiar with the subject but did a little reading on the sun website. Check it outfor more info:http://developer.java.sun.com/developer/technicalArticles/Security/You aren't supposed to be able to break the rules easily at run time. The elimination of pointerarithmetic is supposed to improve type safety.The Code Red worm is a good example of the weakness of the 'old-style' languages. Thisworm uses a 'buffer overflow' attack to 'trick' the IIS program in to doing it's bidding.The Java system is designed to make it harder to make the programming mistake that CodeRed exploits. It is important that you understand that Code Red would not have been possiblewithout careful attention to a fast buck by Mr William Gates III and his cohorts.--Dr HackenbushQ: Does anyone know if there is a way to prevent System.exit() being called?Answer: Look into java.lang.SecurityManager. It has a method called checkExit() with whichyou can prevent System.exit().This method throws a SecurityException if the calling thread is not allowed to cause the JavaVirtual Machine to halt with the specified status code.