1) Unnecessarily Don't override hashCode() method within java code.
2) private, static, final keywords can't be overridden in java.
3) StringBuffer doesn't override the equal() method of an Object class but String class does.
4) when we extend Thread class each and every thread gets associated with new object.
5) When we implement Runnable interface, same object is shared amongst multiple threads.
6) There some illegal combination of modifier, which is never used in java
a) abstract can't be used with ( final , static , private , native , strictfp , synchronized )
b) A transient variable may not be declared as final or static
c) volatile and final combination is always illegal in nature.
d) Native and strictfp combination is illegal for methods in java.
e) The methods cannot be declared native and strictfp simultaneously within java code.
f) final fields cannot be volatile in nature within java.
7) An array is an object in Java. They are not primitive like int, short, or long, but they are also not full featured object with lot of methods, but because they are object, they implicitly extend Object and that's why you can call any method of java.lang.Object using array reference e.g. toString().
8) Initializing array or collection with proper size is still one of the best practices to follow.
for that create one new array and then copy the previous array to newly generated array which still an expensive operation and can slow down the performance of your Java application.
i.e. Array in Java is once created then you can not change the size of the array.
9) Important Points about Constructor in java
a) Constructors can be overloaded.
b) Constructors shouldn't have any return types , not even void.
c) Default constructor is provided by the compiler if we don't declare any constructor. If we declare a parameterized constructor, Compiler will not create any default non parameterized constructor for the class.
d) A constructor can only have either this or super statement and that statement should be the first statement in constructor.
e) If Constructor has been declared private, Objects of the class cannot be instantiated from outside class.
f) If super is not explicitly called, still super() is intrinsically added by the compiler.
g) Default Constructor initializes the instance variables to their default values.
h) Final variables can be initialized within constructor.
10) Important Points about Classes and Interfaces
a) Default constructor initializes the primitives to their default values ( 0 for int , 0.0 for float etc ) and Instance variables to null.
b) Outer Class can't be declared private or protected. It can only be either public or package protected ( default ).
c) All members of Interfaces are intrinsically public which cannot be declared private.
.
d) Variables of an interface are intrinsically public , static and final and can be accessed directly by the implementing class.
e) An object is eligible for garbage collection when it looses all its references.
f) Last Reference assigned null doesn't make the object eligible for gc. Reference should be assigned to some other object.
g) System.gc doesn't enforce garbage collection immediately.
h) All wrapper classes are immutable in nature. But String is not a wrapper class.
i) We don't need object of outer class for accessing static inner class object. Inner static class object can be initiated using outer Class name only.
11) Important Points about Thread
a) Thread.yield() doesn't guarantee that the Thread will keep waiting until execution of other threads.
b) run method can be called directly but in such case it doesn't initiate new thread.
c) invoking start twice on same thread leads to IllegalStateException.
d) Sequence in which multiple runnable threads are executed is system dependent and cannot be predicted.
e) Only methods ( static , instance ) and blocks can be synchronized. Variables cannot be synchronized.
f) InterruptedException is the checked exception for sleep method.
g) sleep is a static method and puts the current thread to sleep for specified time in ms.
h) If the synchronized block is on a string literal, all threads will try to get lock on a common object as its picked from String pool.
i) synchronized cannot be applied to a constructor.
j) wait and notify should be there in synchronized section only.
k) Overridden method run cannot throw any checked exception.
l) There are object spcific locks and class specific locks.
m) suspend() method is used to suspend the execution of a thread for a period of time. We can then restart the thread by using resume() method.
n) yield and sleep are static methods of Thread Class. join is the instance method of Thread Class. wait, notify and notifyAll are methods of Object class.
o) Difference between implementing Runnable interface and extending Thread class
1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance So get your chance through implementing Runnable Interface.
2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.
3) Runnable provides only run() whereas Thread provides start(), run() etc.
4) Separating task as Runnable means we can reuse the task but you can't restart a Thread once it completes.
5) Runnable is loosely coupled whereas Thread no.
6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.
12. Important Points about Exception
a) ArithmeticException is the subclass of RuntimeException and it is an unchecked exception.
b) Compiler gives error if we don't handle checked exceptions whereas unchecked exceptions are optional to be handled.
c) Either we should catch the checked exceptions or the method should be declared to throw those exceptions.
d) Catch blocks for try should handle exceptions from specific to general. Compiler gives error if we have generalize catch before specific.
e) Finally block is always executed whether an exception occurs or not.
f) When an exception is thrown, rest statements in the try block are not executed.
g) try and then finally without catch is legal in java.
h) try without catch or finally is not legal.
i) An overriding method cannot throw an exception wider than the exception being thrown from the overriden method.
13. Important Points about Inner Class
a) Static inner class cannot access instance members of the enclosing class.
b) Local class declared within static method cannot access instance members.
c) Static inner class can be accessed without instance of the parent class.
d) An inner class can access all elements of the parent class , even declared private.
e) To instantiate the inner class object following code is used
OuterClass.InnerClass innerRef = outerRef.new InnerClass();
f) An anonymous inner class can either extend one class or implement one interface only.
g) Anonymous inner class should be ended with a semi colon after curly brace.
h) Static inner class can be instantited like this
OuterClass.InnerClass inner = new OuterClass.InnerClass();
14. Only Integers, char, String and Enum are allowed in Switch statement.
15. If there are no break statements in case blocks, it will execute all statements either till the next break statement or end of switch block (End Of SWT)
16 Default case will be executed if there is no match, and statements thereafter till break or EOS.
17. Important Points about overloading & overriding
a) Methods can be overloaded on number, type and order of arguments.
b) Static methods can be overloaded.
c) Constructors can be overloaded. this( ) is used to call constructors in chain.
d) Methods can't be overloaded on the basis of return type alone.
e) When overloaded on the co variant types, conflict is resolved by the type of reference.
================================================================
f) Overriding Method should have same syntax and return type as Overridden method. If the number and type of arguments varies, it becomes overloading. If the return type is different, compiler gives compilation error i.e "The return type is incompatible with BaseClass.overriddenMethod()".
g) No Explicit cast is required in case of Upcasting i.e derived class reference is assigned to Base class reference but explicit cast is required in case of downcasting i.e Base Class reference is assigned to Derived class reference.
h) Downcasting may throw ClassCastException if object is found to be non compatible during runtime. That's why instanceof check should be made before doing the downcast.
i) Overridding method cannot throw checked exception wider than the exception thrown by the overriden method. If the overridden method doesn't throw any exception, overriding method can throw any exception.
j) We cannot reduce the visibility of the overridden method. Means If the method in base class is public, we cannot declare it private in derived class.
k) Only instance methods can be overridden. Static methods and instance / static variables cannot be overridden.
l) Final methods cannot be overridden.
m) If a method cannot be inherited, It cannot be overridden. So private methods cannot be overridden.
2) private, static, final keywords can't be overridden in java.
3) StringBuffer doesn't override the equal() method of an Object class but String class does.
4) when we extend Thread class each and every thread gets associated with new object.
5) When we implement Runnable interface, same object is shared amongst multiple threads.
6) There some illegal combination of modifier, which is never used in java
a) abstract can't be used with ( final , static , private , native , strictfp , synchronized )
b) A transient variable may not be declared as final or static
c) volatile and final combination is always illegal in nature.
d) Native and strictfp combination is illegal for methods in java.
e) The methods cannot be declared native and strictfp simultaneously within java code.
f) final fields cannot be volatile in nature within java.
7) An array is an object in Java. They are not primitive like int, short, or long, but they are also not full featured object with lot of methods, but because they are object, they implicitly extend Object and that's why you can call any method of java.lang.Object using array reference e.g. toString().
8) Initializing array or collection with proper size is still one of the best practices to follow.
for that create one new array and then copy the previous array to newly generated array which still an expensive operation and can slow down the performance of your Java application.
i.e. Array in Java is once created then you can not change the size of the array.
9) Important Points about Constructor in java
a) Constructors can be overloaded.
b) Constructors shouldn't have any return types , not even void.
c) Default constructor is provided by the compiler if we don't declare any constructor. If we declare a parameterized constructor, Compiler will not create any default non parameterized constructor for the class.
d) A constructor can only have either this or super statement and that statement should be the first statement in constructor.
e) If Constructor has been declared private, Objects of the class cannot be instantiated from outside class.
f) If super is not explicitly called, still super() is intrinsically added by the compiler.
g) Default Constructor initializes the instance variables to their default values.
h) Final variables can be initialized within constructor.
10) Important Points about Classes and Interfaces
a) Default constructor initializes the primitives to their default values ( 0 for int , 0.0 for float etc ) and Instance variables to null.
b) Outer Class can't be declared private or protected. It can only be either public or package protected ( default ).
c) All members of Interfaces are intrinsically public which cannot be declared private.
.
d) Variables of an interface are intrinsically public , static and final and can be accessed directly by the implementing class.
e) An object is eligible for garbage collection when it looses all its references.
f) Last Reference assigned null doesn't make the object eligible for gc. Reference should be assigned to some other object.
g) System.gc doesn't enforce garbage collection immediately.
h) All wrapper classes are immutable in nature. But String is not a wrapper class.
i) We don't need object of outer class for accessing static inner class object. Inner static class object can be initiated using outer Class name only.
11) Important Points about Thread
a) Thread.yield() doesn't guarantee that the Thread will keep waiting until execution of other threads.
b) run method can be called directly but in such case it doesn't initiate new thread.
c) invoking start twice on same thread leads to IllegalStateException.
d) Sequence in which multiple runnable threads are executed is system dependent and cannot be predicted.
e) Only methods ( static , instance ) and blocks can be synchronized. Variables cannot be synchronized.
f) InterruptedException is the checked exception for sleep method.
g) sleep is a static method and puts the current thread to sleep for specified time in ms.
h) If the synchronized block is on a string literal, all threads will try to get lock on a common object as its picked from String pool.
i) synchronized cannot be applied to a constructor.
j) wait and notify should be there in synchronized section only.
k) Overridden method run cannot throw any checked exception.
l) There are object spcific locks and class specific locks.
m) suspend() method is used to suspend the execution of a thread for a period of time. We can then restart the thread by using resume() method.
n) yield and sleep are static methods of Thread Class. join is the instance method of Thread Class. wait, notify and notifyAll are methods of Object class.
o) Difference between implementing Runnable interface and extending Thread class
1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance So get your chance through implementing Runnable Interface.
2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.
3) Runnable provides only run() whereas Thread provides start(), run() etc.
4) Separating task as Runnable means we can reuse the task but you can't restart a Thread once it completes.
5) Runnable is loosely coupled whereas Thread no.
6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.
12. Important Points about Exception
a) ArithmeticException is the subclass of RuntimeException and it is an unchecked exception.
b) Compiler gives error if we don't handle checked exceptions whereas unchecked exceptions are optional to be handled.
c) Either we should catch the checked exceptions or the method should be declared to throw those exceptions.
d) Catch blocks for try should handle exceptions from specific to general. Compiler gives error if we have generalize catch before specific.
e) Finally block is always executed whether an exception occurs or not.
f) When an exception is thrown, rest statements in the try block are not executed.
g) try and then finally without catch is legal in java.
h) try without catch or finally is not legal.
i) An overriding method cannot throw an exception wider than the exception being thrown from the overriden method.
13. Important Points about Inner Class
a) Static inner class cannot access instance members of the enclosing class.
b) Local class declared within static method cannot access instance members.
c) Static inner class can be accessed without instance of the parent class.
d) An inner class can access all elements of the parent class , even declared private.
e) To instantiate the inner class object following code is used
OuterClass.InnerClass innerRef = outerRef.new InnerClass();
f) An anonymous inner class can either extend one class or implement one interface only.
g) Anonymous inner class should be ended with a semi colon after curly brace.
h) Static inner class can be instantited like this
OuterClass.InnerClass inner = new OuterClass.InnerClass();
14. Only Integers, char, String and Enum are allowed in Switch statement.
15. If there are no break statements in case blocks, it will execute all statements either till the next break statement or end of switch block (End Of SWT)
16 Default case will be executed if there is no match, and statements thereafter till break or EOS.
17. Important Points about overloading & overriding
a) Methods can be overloaded on number, type and order of arguments.
b) Static methods can be overloaded.
c) Constructors can be overloaded. this( ) is used to call constructors in chain.
d) Methods can't be overloaded on the basis of return type alone.
e) When overloaded on the co variant types, conflict is resolved by the type of reference.
================================================================
f) Overriding Method should have same syntax and return type as Overridden method. If the number and type of arguments varies, it becomes overloading. If the return type is different, compiler gives compilation error i.e "The return type is incompatible with BaseClass.overriddenMethod()".
g) No Explicit cast is required in case of Upcasting i.e derived class reference is assigned to Base class reference but explicit cast is required in case of downcasting i.e Base Class reference is assigned to Derived class reference.
h) Downcasting may throw ClassCastException if object is found to be non compatible during runtime. That's why instanceof check should be made before doing the downcast.
i) Overridding method cannot throw checked exception wider than the exception thrown by the overriden method. If the overridden method doesn't throw any exception, overriding method can throw any exception.
j) We cannot reduce the visibility of the overridden method. Means If the method in base class is public, we cannot declare it private in derived class.
k) Only instance methods can be overridden. Static methods and instance / static variables cannot be overridden.
l) Final methods cannot be overridden.
m) If a method cannot be inherited, It cannot be overridden. So private methods cannot be overridden.
No comments:
Post a Comment