-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJavaQuestions.txt
More file actions
83 lines (54 loc) · 5.19 KB
/
Copy pathJavaQuestions.txt
File metadata and controls
83 lines (54 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Q : What is the use of package in java?
Packages in Java are the way to organize files when a project has many modules. One important benefit of such an organization of files in different modules is that it helps resolving naming conflicts i.e., different packages may have classes with the same name. Obviously this is not possible if we don’t have the concept of packages. In that we need to find different names for all the classes, which may not be desirable in every case. Plus, packages improve making the application more modular.
Q : Method overriding rules in JAVA
1) The signature including the number and type of arguments for the overriding method should be same.
2) The return type should be same.
3) [Inheritance] The access modifier for overriden method in child class should not be more limiting than superclass method.
4) [Inheritance, exceptions] Superclass method DOES NOT DECLARE AN EXCEPTOIN
Rule 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.
Rule 2: If the superclass method does not declare an exception, subclass overridden method can declare unchecked exception.
5) [Inheritance, exceptions] Superclass method DECLARES an EXCEPTION
Rule: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
6) Static methods in parent class can not be overriden in sub-class. Sub-class method does method hiding of the parent class method. Run time polymorphism will not work.
Q : How to write Java documentation ?
Included in Sun’s Java Development Kit (JDK) is a program called javadoc that processes Java code files and produces external documentation, in the form of HTML files, for your Java programs.
Tag Used for Purpose
-------------------------------------------------------------------------------
@author name Interfaces,
Classes, Indicates the author(s) of a given piece of code.
One tag per author should be used.
@deprecated Interfaces,
Classes,
Member Functions Indicates that the API for the class… has been deprecated and therefore should not be used any more.
@exception name description Member Functions Describes the exceptions that a member function throws. You should use one tag per exception and give the full
class name for the exception.
@param name description Member Functions Used to describe a parameter passed to a member function, including its type/class and its usage.
Use one tag per parameter.
@return description Member Functions Describes the return value, if any, of a member function. You should indicate the type/class and
the potential use(s) of the return value.
@since Interfaces,
Classes,
Member Functions Indicates how long the item has existed, i.e. since JDK 1.1
@see ClassName Classes,
Interfaces,
Member Functions,
Fields Generates a hypertext link in the documentation to the specified class.
You can, and probably should, use a fully qualified class name.
@see
ClassName#memberfunctionName Classes,
Interfaces,
Member Functions,
Fields Generates a hypertext link in the documentation to the specified member function.
You can, and probably should, use a fully qualified class name.
@version text Classes,
Interfaces Indicates the version information for a given piece of code.
-------------------------------------------------------------------------------
[LOCAL VARIABLES : inside method] - final
[OUTER CLASSES] - default, public, final
[LOCAL VARIABLES] Why final is the only modifier for the local variable in java?
In short - none of the other modifiers make sense in that context. Saying a variable is public, private, protected, or static simply doesn't make sense in the context of a local variable that will go out of scope (and be garbage collected) once the method exits. Those modifiers are intended for class fields (and methods), to define their visibility (or in the case of static, their scope).
final is the only one that makes sense in the context of a local variable because all it means is that the variable cannot be modified after its initial declaration, it has nothing to do with access control.
[CLASS - OUTER CLASS] Why can't a Java class be declared as static?
In Java, the static keyword(defines the scope) typically flags a method or field as existing not once per instance of a class, but once ever. A class exists once anyway so in effect, all classes are "static" in this way and all objects are instances of classes.
static does have a meaning for inner classes, which is entirely different.
An inner class is by default not static, which is to say that each instance of a class that has a nested class gets its own copy of the nested class. This is how you can reference "this" in the class, and why you can't declare static methods or fields, doesn't make any sense, since each instance of the outer class has its own copy. A "static" nested class (by definition a true "inner" class) means there is only one copy of that class.