Object design
What's the difference between a class and an object in Java?
A: A Java class is a definition or model of an object type. A class has a specific set of fields, methods and constructors with distinct argument types. Any object that fulfils a class definition directly or by inheritance has a set of properties and behaviour that is common to all instances of the class. In this sense, a class is like a set of things that are alike.
In Java concrete classes also provide a code implementation that can be instantiated to create an object reference. An instance of a class directly fulfils the its own definition, it also fulfils any superclass definitions too.
The Java Virtual Machine creates static references to classes when it runs a Java program. Classloaders make the public static fields and methods of classes available to the runtime system whether any instance exists or not. When a constructor is called, the class returns an instance of the object it represents.
What is an object in Java?
A: In Java the Object class is the ultimate superclass of every other object type. All objects are extended from the Object class, either directly or by inheritance through any number of parent classes. If a class does not explicitly extend any named class, it implicitly extends the Object class. An object with a small o is the word used to describe an instance of a Java class.
What is the difference between a method header and its signature?
A: A Java method header is the whole declaration statement for a method before its curly braces. The header includes the method's visibility modifier, return type, arguments and exceptions, as below.
public final String getDetails(final File file,
final String key) throws IOException
A Java method signature is the method name and parameters only. The order of the parameters is significant because they may distinguish overloaded methods by the same name.
getDetails(File, String)
Can I use the same variable name in two methods?
A: It is possible to use the same variable names for method local variables in two separate methods. However, all variables in a single method must have different names.
public class MethodLocalVariables {
void testOne() {
int test;
}
void testTwo() {
int test;
}
}
Can objects be used in place of arrays?
A: Yes, sometimes it is sensible to use an object to carry other object references instead of an array. For instance, you could issue an object as the return value of a method that must return multiple object references.
What's the difference between equals and ==?
A: The Java == operator is used to compare primitive values such as int, long and boolean for equality; whether the variables, values or expression on either side of the operator equate to the same value.
No comments:
Post a Comment