Tuesday, January 30, 2007

Important Points in SCJP Preparation

Important Points in SCJP Preparation
Some important tipsConstructors:1. If the sub class constructor doesn’t call any super class or its other constructor explicitlya. If the super class has default constructor(explicitly or implicitly and visible), theni. super() is invoked implicitly if no exception is thrown or the sub class constructor throws that exceptionii. Compiler error otherwise.b. Compiler error otherwise.2. If the sub class constructor call super class constructor explicitly(using super keyword)a. If the super class has such a constructor and visible, then it is calledb. Compiler error otherwise.3. If the sub class constructor call its other constructor explicitly(using this keyword)a. If the sub class has such a constructor, then it is calledb. Compiler error otherwise.4. The super or this constructor call can be only the first line of any constructor and only one of them can be used at a time. As to resolve the point, when to call the base class constructor and to call what base class constructor.Overriding:1. The static methods can’t be overridden at all.a. The static methods are resolved and invoked with reference type and not with the type of object the reference point to.2. The private methods are not overridden at all.a. The private method even with the same signature as base class method is a new method in the super class.3. The protected and public can be overriddena. But the visibility can’t be reducedb. Can throw only the same exception or any sub class of the exception that is thrown by the base class hidden methodc. These are resolved and invoked with the type of object the reference point to.

Overloading:1. The method name is the name2. The signature is differenta. The number of parametersb. The type of parameters3. These are resolved asa. Look for the functions with appropriate number of parametersb. Match for the type of each parameteri. If exact type of the parameter is not found,1. Then search for its super class types(due to multiple inheritance thru interface)a. If there are more than one found, then Compiler error as ambiguity2. Compiler error if none foundc. Compiler error if none found

http://www.javadeveloper.co.in/scjp/scjp-mock-exams.html.....mock exams


Declarations and Access Control

Modifiers fall into two categories:
* Access modifiers:public,protected,private
* Nonaccess modifiers (includingstrictfp,final, andabstract)
* A class can be declared with only public or default access. (Additionally Strictfp, abstract, final)
* The other two access control levels protected,private don’t make sense for a class
* Four access controls (which means all three modifiers + default) work for most method and variable declarations
Class Access (Visibility)
* Default Access: A class with default access can be seen only by classes within the same package.
* Note: If class A and class B are in different packages, and class A has a default access, class B won’t be able to create an instance of class A, or even declare a variable or a return type of class A.
* Do these two things to make it work, place class A, class B in the same package (or) declare class A with public access modifier.
* Exam Watch : When you see a question with complex logic, be sure to look at the access modifiers first. That way, if you spot an access violation (for example, a class n package A trying to access a default class in package B), you’ll know the code won’t compile so you don’t have to bother working through the logic. It’s not as if, you know, you don’t have anything better to do with your time while taking the exam. Just choose the “Compilation fails” answer and zoom on to the next question.
* Public Access: A class declaration with the public keyword gives all classes from all packages access to the public class. - visible to all classes in all packages.
Other (Nonaccess) Class Modifiers:
* Can modify a class declaration using the keyword final,abstract, or strictfp.
* These modifiers are in addition to whatever access control is on the class. You can’t always mix non abstract modifiers.
* never, ever, ever mark a class as both final and abstract. (Tell me why?)
* strictfp is a keyword and can be used to modify a class or a method, but never a variable.
* Marking a class as strictfp means that any method code in the class will conform to the IEEE754 standard rules for floating points.
* Final Classes When used in a class declaration, the final keyword means the class can’t be sub classed. No other class can ever extend (inherit from) a final class.
* Final can be used to modify a class or a method or a variable.
* Abstract Classes: An abstract class can never be instantiated. It is to be extended (subclassed).
* Abstract can be used to modify a class, a method but never a variable.
* Exam Watch: Look for questions with a method declaration that ends with a semicolon, rather than curly braces. If the method is in a class—as opposed to an interface—then both the method and the class must be marked abstract. You might get a question that asks how you could fix a code sample that includes a method ending in a semicolon, but without an abstract modifier on the class or method. In that case, you could either mark the method and class abstract, or remove the abstract modifier from the method. Oh, and if you change a method from abstract to non abstract, don’t forget to change the semicolon at the end of the method declaration into a curly brace pair!
* Remember: Even a single method is abstract, the whole class must be declared abstract.
* Can put non abstract methods in the abstract class.
* Exam Watch: You can’t mark a class as both abstract and final. They have nearly opposite meanings. An abstract class must be subclassed, whereas a final class must not be subclassed. If you see this combination of abstract and final modifiers, used for a class or method declaration, the code will not compile.
Method and Variable Declarations and Modifiers
* Methods and instance (nonlocal) variables are collectively known as members. Can modify a member with both access and non-access modifiers.
* Member Access: public, protected, private, default.
* Exam Watch: It’s crucial that you know access control inside and out for the exam. There will be quite a few questions with access control playing a role. Some questions test several concepts of access control at the same time, so not knowing one small part of access control could blow an entire question.
* Question: What does it mean for code in one class to have access to a member of another class? The member in class A should be visible to class B
* You need to understand two different access issues: 1. Whether method code in one class can access a member of another class. . (Using reference). 2.Whether a subclass can inherit a member of its superclass (use extend and access it by inheritance iff the member should be visible)
* If a subclass inherits a member, the subclass has the member.
* Exam Watch: You need to know the effect of different combinations of class and member access (such as a default class with a public variable). To figure this out, first look at the access level of the class. If the class itself will not be visible to
another class, then none of the members will be either, even if the member is declared public. Once you’ve confirmed that the class is visible, then it makes sense to look at access levels on individual members.
* Public: When a method or variable member is declared public, it means all other class regardless of the package can access the member.(assuming the class itself is visible).
* If a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.
* In the subclass, you can invoke the super class method without reference. (Without the dot operator). Using reference also wont hurt.
* The reference this always refers to the currently executing object. this reference is implicit.
* Private: Members marked private can’t be accessed by code in any class other than the class in which the private member is declared.
* A private member is invisible to any code outside the member’s own class.
* Question: What about a subclass that tries to inherit a private member of its superclass? A subclass can’t inherit it.
* A subclass can’t see or use or even think about private members of its super class.
* You can however have a matching method in the subclass with the same method name in the super class. It is simply a method that happens to have the same method name as the private method in the super class. Overriding rules does not apply.
* On the job: In practice it’s nearly always best to keep all variables private or protected. If variables needs to be changed programmers should use public accessor methods.
* Question: Can a private method be overridden by a subclass? No. Since the subclass cannot inherit the private method, it therefore cannot override the method. (Overriding depends on inheritance).
* Default Members: A default member can be accessed only if the class accessing the member belongs to the same package.
* Protected Members: A protected member can be accessed (through inheritance) by a subclass even if the subclass is in the different package.
* Note: You cannot access protected member using object reference. The subclass cannot use the dot operator on the superclass reference to access the protected member.
* Question But what does it mean for a subclass-outside the package to have access (visibility to a superclass (parent) member? It means the subclass inherits the member.
* The subclass can only see the protected member through inheritance. For a subclass outside the package, the protected member can be accessed only through inheritance.
* Question: What does a protected member look like to other classes trying to use the subclass-outside-the-package to get the subclass’ inherited protected superclass member?
Say for ex: class A is in Package A, class B is in Package B. class B is accessing the protected member x in class A. What happens if some class say class C in the same package B has a reference to class B wants to access the member variable x (protected member in class A).
How does that protected member behave once the subclass has inherited it? Does it maintain its protected status, such that classes in the Child’s package can see it?
No. Once the subclass-outside-the package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass.
The bottom line: when a subclass-outside-the-package inherits a protected member, the member is essentially private inside the subclass, such that only the subclass’ own code can access it.
* Question: What about default access for two classes in the same package? It works fine. Remember that default members are visible only to the subclasses are in the same package as the superclass.
Local Variables and Access Modifiers
* Question: Can access modifiers be applied to local variables? No.
* Exam Watch: There is never a case where an access modifier can be applied to a local variable, so watch out code like the following.
class Foo {
void doStuff() {
private int x = 7;
this.doMore(x);
}
}
You can be certain that any local variable declared with modifier cannot compile. In fact there is only one modifier can ever be applied to a local variables-final.
Visibility
Public
Protected
Default
Private
From the same class
Yes
Yes
Yes
Yes
From any class in the same package
Yes
Yes
Yes
No
From any non-subclass class outside the package.
Yes
No
No
No
From a subclass in the same package
Yes
Yes
Yes
No
From a subclass outside the same package.
Yes
Yes
No
No

NonAccess Member Modifiers
* Final, abstract, transient, synchronized, strictfp, native.
* Final Methods: The final keyword prevents a method from being overridden in a subclass.
* Final Arguments: Method arguments are essentially the same as the local variables. Can declare the method arguments as final.
* Abstract Methods: An abstract method is a method that has been declared but has to be implemented. This method contains no functional code. An abstract method does not even have the curly braces for the functional implementation, instead it closes with the semicolon.
* You can have abstract class with no abstract methods.
* Any class that extends abstract class has to implement all abstract methods of the superclass. Unless the subclass is also abstract.
* Rule is: The first concrete (means Non abstract) subclass of an abstract class must implement all abstract methods of the superclass.
* If the subclass is abstract it is not required to implement the abstract methods of the superclass, but it is allowed to implement any or all of the superclass abstract methods.
* Exam Watch: Look for concrete classes that don’t provide method implementations for abstract methods of the superclass. For example, the following code won’t compile:
public abstract class A {
abstract void foo();
}
class B extends A {
void foo(int I) {
}
}
Class B won’t compile because it doesn’t implement the inherited abstract
method foo(). Although the foo(int I)method in class B might appear
to be an implementation of the superclass’ abstract method, it is simply
an overloaded method (a method using the same identifier, but different
arguments), so it doesn’t fulfill the requirements for implementing the
superclass’ abstract method.
* Note: A method can never ever be marked as both abstract and final or both abstract and private. Think!!
Abstract methods has to be implemented, whereas final and private methods cannot be overridden by the subclass.
* Abstract methods cannot be marked as synchronized, strictfp or native, static.
* Synchronized Methods: The synchronized keyword indicates that the method can be accessed by only one thread at a time.
* Synchronized keyword can be applied only to methods.
* Synchronized modifier can be paired with any of the four access control levels. (private, public, default, protected).
* Synchronized can be paired with final but not with abstract
* Native Methods: The native keyword indicates that a method is implemented in a platform dependent language such as C.
* Native can never be combined with abstract.
* Native can be applied only to methods, not to classes , variables.
* Strictfp Methods: Strictfp forces floating point operations to adhere IEE754 standard.
* Strictfp can modify a class or nonabstract method declaration and that a Variable can never be declared strictfp.
Variable Declarations
* Instance Variables: Instance variables are defined inside the class but outside the method. Onlu initialized when the class is instantiated.
* Instance variables can use any of the four access levels. Can be marked Final, transient, abstract, strictfp, native, synchronized.
* Local variables: Local variables are declared within the method. Means variables is not just initialized within the method but also declared within the method. It starts its life inside the method, it’s also destroyed when the method has completed.
* Local variables are always on the stack, not the heap. Get clarified. Pg:34
* Local variable declarations can’t use most of the modifiers such as public, transient, volatile, abstract, static.
* Local variables can be marked final.
* Before using the local variable it should be initialized.
* Local variables don’t get default values.
* Local variables can’t be referenced outside the method in which it is declared.
* It is possible to declare the local variable with the same name as an instance variable. That’s known as shadowing.
The following (but wrong) code is trying to set an instance variable’s value using a parameter:
class Foo {
int size = 27;
public void setSize(int size) {
size = size; // ??? which size equals which size???
}
}
Use the keyword this. The this keyword always always refers to the object currently running.
* Final Variables: Impossible to reinitialize the variable once it has been initialized. For primitives, once the variable is assigned a value, the value can’t be altered.
* Question: What does it mean to have a final object reference? A reference variable marked final can’t ever be reassigned to refer to a different object. The data within the object, however can be modified but the reference variable cannot be changed.
* You can use final references to modify the object it refers to, but you can’t modify the reference variable to make it refer to a different object.
* Remember there are no final objects, only final references.
* Exam Watch: Look for code that tries to reassign a final variable, but don’t expect to be obvious. For example, a variable declared in an interface is always implicitly final, whether you declare it that way or not. So you might see code similar to the following:
interface Foo {
Integer x = new Integer(5); // x is implicitly final
}
class FooImpl implements Foo {
void doStuff() {
x = new Integer(5); // Big Trouble! Can’t assign new object to x
}
}
The reference variable x is final. It is an interface variable, they are always implicitly public static final. Final can’t be reassigned, that in the case of interface variables, they are final even if they don’t say it out loud. The exam expects you to spot any attempt to violate this rule.
* Transient Variables: If you mark an instance variable as transient, (Remember: only you can declare final to the local variable) you’re telling the JVM to skip this variable when you attempt to serialize the object declaring it.
* Serialization lets u save an object by writing its state to a special IO writing stream.
* For exam you need know: Transient can be applied only to instance variables.
* Volatile Variables: The volatile tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in the memory.
* For exam you need know: Volatile can be applied only to instance variables.
* Static variables and methods: Can use a static method or a variable without having any instances of that class at all.
* Variables and methods marked static belong to the class.
* The rule is, a static method of a class can’t access a nonstatic (instance) member—method or variable—of its own class.
* Exam Watch: One of the mistakes most often made by new Java programmers is attempting to access an instance variable (which means nonstatic variable) from the static main()method (which doesn’t know anything about any instances, so it can’t access the variable). The following code is an example of illegal access of a nonstatic variable from a static method:
class Foo {
int x = 3;
public static void main (String [] args) {
System.out.println(“x is “ + x);
}
}
Understand that this code will never compile, because you can’t access a
nonstatic (instance) variable from a static method. Just think of the compilersaying, “Hey, I have no idea which Foo object’s x variable you’re trying to print!”
Remember, it’s the class running the main()method, not an instance of the
class. Of course, the tricky part for the exam is that the question won’t look
as obvious as the preceding code. The problem you’re being tested for—
accessing a nonstatic variable from a static method—will be buried in code
that might appear to be testing something else.

For example, the code above would be more likely to appear as
class Foo {
int x = 3;
float y = 4.3f;
public static void main (String [] args) {
for (int z = x; z < ++x; z--, y = y + z) { // complicated looping and branching code } } So while you’re off trying to follow the logic, the real issue is that x and y can’t be used within main(), because x and y are instance, not static, variables! The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class can’t access a nonstatic (instance) member— method or variable—of its own class. v Accessing Static Methods and Variables: Access a static method or static variable is to use the dot operator on the class name, as opposed to on a reference to an instance variable. v Also java allows you to access the static members using the object reference variable. v Static methods can’t be overridden. v Things you can mark as Static: Methods, Variables, Top-level nested classes. v Things you can’t mark as Static: Constructors (constructor is used only to create instances), Classes, Interfaces, Inner Classes, Inner class methods and variables, Local variables. v Static method cannot access an instance (non-static) variable. (remember about main method) v Static method cannot access a non-static methods. v Static method can access a static method and variable. v Static members are per-class as opposed to per-instance. Source files, Package, Declarations and Import Statements. v There can be only one package statement per source code file. v Can have only one pubic class per source code file. v On the Job: Putting multiple classes in the same source code file makes it much harder to locate the source for a particular class and makes the source code less reusable. v Exam Watch: The exam uses a line numbering scheme that indicates whether the code in the question is a snippet (a partial code sample taken from a larger file), or complete file. If the line numbers start at 1, you’re looking at a complete file. If the numbers start at some arbitrary (but always greater than 1) number, you’re looking at only a fragment of code rather than the complete source code file. For example, the following indicates a complete file: 1. package fluffy; 2. class Bunny { 3. public void hop() { } 4. } whereas the following indicates a snippet: 9. public void hop() { 10. System.out.println(“hopping”); 11. } v Exam Watch: You might see questions that appear to be asking about classes and packages in the core Java API that you haven’t studied, because you didn’t think they were part of the exam objectives. For example, if you see a question like class Foo extends java.rmi.UnicastRemoteObject { /// more code } don’t panic! You’re not actually being tested on your RMI knowledge, but rather a language and/or syntax issue. If you see code that references a class you’re not familiar with, you can assume you’re being tested on the way in which the code is structured, as opposed to what the class actually does. In the preceding code example, the question might really be about whether you need an import statement if you use the fully qualified name in your code (the answer is no, by the way). v Exam Watch: Look for syntax errors on import statements. Can you spot what’s wrong with the following code? import java.util.Arraylist.*; // Wildcard import import java.util; // Explicit class import The first import looks like it should be a valid wildcard import, but ArrayList is a class, not a package, so it makes no sense (not to mention making the compiler cranky) to use the wildcard import on a single class. Pay attention to the syntax detail of the import statement, by looking at how the statement ends. If it ends with .*;(dot, asterisk, semicolon), then it must be a wildcard statement; therefore, the thing immediately preceding the .*;must be a package name, not a class name. Conversely, the second import looks like an explicit class import, but util is a package, not a class, so you can’t end that statement with a semicolon. v Question: What happens if you have two classes with the same name in two different packages, and you want to use both in the same source code? You should use fully qualified name. v static public void main (String whatever []) { } – This method is perfectly legal. The most important point for the exam is to know that not having the “able-to-run” main() method is a runtime, rather than compiler error. A class with a legal, nonstatic main()method, for example, will compile just fine, and other code is free to call that method. But when it comes time to use that class to invoke the JVM, that nonstatic main()method just won’t cut it, and you’ll get the runtime error. v Interface Implementation: Exam Watch: You must know how to implement the java.lang.Runnableinterface,without being shown the code in the question. In other words, you might be asked to choose from a list of six classes which one provides a correct implementation of Runnable. Be sure you memorize the signature of the one and only one Runnableinterface method: public void run() { } For any other interface-related question not dealing with Runnable, if the specification of the interface matters, the interface code will appear in the question. A question, for example, might show you a complete interface and a complete class, and ask you to choose whether or not the class correctly implements the interface. But if the question is about Runnable, you won’t be shown the interface. You’re expected to have Runnablememorized! v Interface: What a class can do without saying anything about how the class will do it. v Think of an interface as a 100-percent abstract class. Like an abstract class interface defines abstract methods. v But while an abstract class can define both abstract and non-abstract methods. v Interface can have only abstract methods. v Interface has very little flexibility in how the methods and variables defined in the interface are declared. The rules are strict, but simple. v All interface methods must be implemented and must be marked public. v What you declare in the interface : interface bounceable{ void bounce(); void setbouncefactor(int bf); } v What the compiler looks: interface bounceable{ public abstract void bounce(); public abstract void setbouncefactor(int bf); } v All interface methods are implicitly public and abstract. v Interface methods must not be static. v You do not need to actually type the public and abstract modifiers in the method declaration, but the method is still always public and abstract. v All variables defined in the method must be public static final. In other words, interfaces can declare only constants, not instance variables. v Because interface methods are abstract, they cannot be marked as final, native, synchronized, strictfp. v An interface can extend one or more interfaces. v An interface cannot extend anything but another interface. v An interface cannot implement another interface or class. v Interface must be declared with the keyword interface. v public interface Rollable {} , public abstract interface Rollable { } – Both are legal. Since interfaces are implicitly abstract. v Exam Watch: Look for interface methods declared with any combination of public,abstract, or no modifiers. For example, the following five method declarations, if declared within an interface, are legal and identical! void bounce(); public void bounce(); abstract void bounce(); public abstract void bounce(); abstract public void bounce(); The following interface method declarations won’t compile: final void bounce(); // final and abstract can never be used static void bounce(); // interfaces define instance methods private void bounce(); // interface methods are always public protected void bounce(); // (same as above) synchronized void bounce(); // can’t mix abstract and synchronized native void bounce(); // can’t mix abstract and native strictfp void bounce(); // can’t mix abstract and strictfp v By placing the constants in the interface, any class that implements the interface has direct access to the constants, just as if the class had inherited them. v Interface constants must always be public, static, final. v Any variable declared in the interface must be implicitly is – a public constant. v Exam Watch: Look for interface definitions that define constants, but without explicitly using the required modifiers. For example, the following are all identical: public int x = 1; // Looks non-static and non-final, but isn’t! int x = 1; // Looks default, non-final, and non-static, but isn’t! static int x = 1; // Doesn’t show final or public final int x = 1; // Doesn’t show static or public public static int x = 1; // Doesn’t show final public final int x = 1; // Doesn’t show static static final int x = 1 // Doesn’t show public public static final int x = 1; // Exactly what you get implicitly Any combination of the required (but implicit) modifiers is legal, as is using no modifiers at all! On the exam, you can expect to see questions you won’t be able to answer correctly unless you know, for example, that an interface variable is final and can never be given a value by the implementing (or any other) class. v While implementing the interface – Provide concrete implementations for all methods from the declared interface. – Follow all the rules for legal overrides – Declare no checked exceptions on implementation methods other than those declared by the interface methods or subclass of those declared by the interface method. – Maintain the signature of the interface method, and maintains the same return type. But does not have to declare the exceptions declared in the interface method declaration. v An implementation class can itself be abstract. When the implementation class itself is abstract, it simply passes the buck to its first concrete subclass. v Exam Watch: Look for methods that claim to implement an interface but don’t provide the correct method implementations. Unless the implementing class is abstract, the implementing class must provide implementations for all methods defined in the interface. v A class can extend more than one interface You can extend only one class but implement many interface. v An interface can extend another interface, but cannot implement another interface. v public interface Bounceable extends Moveable { } - Perfectly legal. The first concrete implementation class of Bounceable interface, should implement all the methods of Bounceable Plus all the methods in Moveable interface. v An interface can extend more than one interface. Think!!! Question: A class is not allowed to extend multiple classes. If it does then it would be multiple inheritance But why in Interface interface Bounceable extends Moveable, spherical { void bounce(); void setBounceFactor(int bf); } class Ball extends Bounceable – this class has to implement all the methods in Bounceable, Moveable, Spherical. If not then class Ball is marked abstract. v Exam Watch: Look for illegal use of extends and implements. The following shows examples of legal and illegal class and interface declarations. 1. class Foo { } 2. class Bar implements Foo { } 3. interface Baz { } 4. interface Fi { } 5. interface Fee implements Baz { } 6. interface Zee implements Foo { } 7. interface Zoo extends Foo { } 8. interface Boo extends Fi { } 9. class Toon extends Foo, Button { } 10. class Zoom implements Fi, Fee { } 11. interface Vroom extends Fi, Fee { } 1. Ok. 2. No. Class cannot implement another class. 3. Ok. 4. Ok. 5. No. Interface cannot implement another interface. It can only extend another interfaces. 6. No. Interface cannot implement another class. 7. No. Interface cannot extend class. It will extend only interfaces. 8. Ok. 9. No. Class cannot extend two classes. 10. Ok. Class can implement multiple interface. 11. Ok. interface can extend multiple interfaces. Burn these in, and watch for abuses in the questions you get on the exam. Regardless of what the question appears to be testing, the real problem might be the class or interface declaration. Before you get caught up in, say, tracing a complex threading flow, check to see if the code will even compile. (Just that tip alone may be worth your putting us in your will!) (You’ll be impressed by the effort the exam developers put into distracting you from the real problem.) (How did people manage to write anything before parentheses were (was?) invented?) v v Assertions: Let you to test your assumptions during development, without the expense (in both your time and program overhead) of writing exception handlers for exceptions that you assume will never happen once the program is out of development and fully deployed. v v