General Java Questions - V

General Java Questions - V

Q:
"Also, a subclass cannot override methods that are declared static in the superclass. In other
words, a subclass cannot override a class method. A subclass can HIDE a static method in the
superclass by declaring a static method in the subclass with the same signature as the static
method 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 that
method 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 main
points 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 - V
static 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 names
instead 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 to
binary code by JVM, and executed by the machine
Question: I know that void method does not return any value. But I still write the code like
this:
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 in
java.*. SUN does not provide documentation for sun.* because those are the Sun-specific
implementation, and specifically not part of the Java technology API standard, and are
therefore 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 - V
In general, SUN doesn’t provide javadoc documentation for sun.* classes in order to
discourage developers from writing programs that use them. For further explanation, see the
next 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.net
are in the source files located at:
/src/share/sun/sun/net/*.java
This source code release does not include javadoc-generated documentation. You would have
to generate those docs yourself using javadoc.
source: http://java.sun.com/products/jdk/faq.html#A12
Q: 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 and
parse.
So the designers of C decided to have only 1 form of defining variables with pointer types or
array 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 - V
Java carried over the variable binding, but also introduced the type binding, because there
were no more pointers, so the ambiguity was removed.
--
Joona Palaste
And 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 of
C++.
* Increased its pool of programmers by attracting C/C++ programmers through the effects
above.
This isn't the only instance of anachronistic syntax; consider the optional semicolon at the end
of 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 name
but 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 overloaded
public 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 much
better explanation.
--
Anthony Miller
file:///C|/330_new/330_new/general_java-V.htm (5 of 6) [2003-07-22 22:07:56]
General Java Questions - V
Q: Why people claim that java is a secure language...
Answer: The java programming language itself is just as secure or insecure as any other
programming language. A Java Applet (which are used on the web) is a different matter. They
are quite secure.
An applet does not have access to system resources outside of the directory that it is created
in. There are ways that you can give an applet access to this information, but they are pretty
explicit 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 out
for 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 pointer
arithmetic is supposed to improve type safety.
The Code Red worm is a good example of the weakness of the 'old-style' languages. This
worm 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 Code
Red exploits. It is important that you understand that Code Red would not have been possible
without careful attention to a fast buck by Mr William Gates III and his cohorts.
--
Dr Hackenbush
Q: 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 which
you can prevent System.exit().
This method throws a SecurityException if the calling thread is not allowed to cause the Java
Virtual Machine to halt with the specified status code.

No comments: