Modifiers

                                                Java modifiers
  
  1. For the top level classes which modifies are allowed?
·        Java allows top level classes / interfaces to be public or default only.
·        public, default, final, abstract, strictfp.
  2. Is it possible to declare a class as static, private, protected?
Top level classes can’t be declared as static, private, protected.
·        Private top-level class does not make any sense because you can't access it from anywhere.
·        Protected means to access class within the same package or subclass of the outer class. Since there isn't package inheritance in java then protected classes also does not make any sense.
·        When we declare method/parameter as static, then we can access it without creating an instance of object. Because static member belongs to the class as a whole, not the instance of class or object. Since there isn't enclosing class for top-level classes, it is meaningless to define top-level classes as static.

 3. What are extra modifiers applicable inner classes when compared with outer   classes?
     Private, protected, static
 4. What is final class?
·        A final class cannot be extended by any other class
·        A final variable cannot be reassigned to another value
·        A final method cannot be overridden
 5.Explain the difference between final, finally, finalize?
final:
final is a keyword.
The variable declared as final should be initialized only once and cannot be changed.
Java classes declared as final cannot be extended.
Methods declared as final cannot be overridden.
finally:
Finally is a block.
The finally block always executes when the try block exits.
This ensures that the finally block is executed even if an unexpected exception occurs.
Anything inside of the "finally" clause will be executed regardless of if the code in the 'try' block throws an exception or not.
Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
finalize:
finalize is a method.
 Before an object is garbage collected, the runtime system calls its finalize() method.
You can write system resources release code in finalize() method before getting garbage collected.
   6. Is every method present in final class is final?
 You can't extend a final class, so you can't override its methods. It doesn't really make sense     to talk about the methods being final or not.
   7. Is every variable present inside a final class is final?
No
   8. What is abstract class?
·        Abstract class is one which can't be instantiated, i.e. its object cannot be created.
·        A Non-abstract class can only have Non-abstract methods.
·        An Abstract class can have both the Non-abstract as well as Abstract methods.
·        An Abstract method must be implemented by the very first Non-Abstract sub-class.
·        A class which has abstract method must be declared as abstract.
    9. What is abstract method?
Abstract methods are method's declaration without its definition.
   10. If a class contains at least one abstract method is it required to declare that class compulsory abstract?
Yes, a class which has abstract method must be declared as abstract.
11.If a class doesn’t contain any abstract methods is it possible to declare that class as abstract?
Yes
12.Whenever we are extending abstract class is it compulsory required to provide implementation for every method of that class?
Yes
13.Is final class can contain abstract method?
No, a class  can be either abstract or final, but not both
But an abstract class can have a final method in Java
14. Is final class can contain final method?
yes
     15.Can u give example for abstract class which doesn’t contain any method?
16.Which of the following modifiers combinations are legal for methods?
Public-static- valid
Static-abstract – No, You create abstract methods because you want your child classes to override them but static methods are associated with classes not their instance so they can't be overridden.
Abstract –final - no
Final-synchronized -yes
Synchronized-native -Yes
Native-abstract- no
  17. Which of the following modifiers combinations are legal for classes?
Public-final
Final-abstract
Abstract-strictfp
Strictfp-public
18. What is the difference between abstract class & interface?

abstract Classes
Interfaces


1
 abstract class can extend only one class or one abstract class at a time
 interface can extend any number of interfaces at a time

2
 abstract  class  can extend from a class or from an abstract class
 interface can extend only from an interface

3
 abstract  class  can  have  both  abstract and concrete methods
 interface can  have only abstract methods

4
 A class can extend only one abstract class
 A class can implement any number of interfaces

5
 In abstract class keyword ‘abstract’ is mandatory to declare a method as an abstract
 In an interface keyword ‘abstract’ is optional to declare a method as an abstract

6
 abstract  class can have  protected , public and public abstract methods
 Interface can have only public abstract methods i.e. by default

7
 abstract class can have  static, final  or static final  variable with any access specifier
 interface  can  have only static final (constant) variable i.e. by default







When to use abstract class and interface in Java
Here are some guidelines on when to use an abstract class and when to use interfaces in Java:
·        An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
·        An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.
·        If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
·        Interfaces are a good choice when you think that the API will not change for a while.
·        Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces

19. What is strictfp modifier?
20. Is it possible to declare a variable with strictfp?
No
21. Abstract –strictfp combination, is legal for classes or methods?
22. Is it possible a native method?
Yes
23. What is the difference between instance& static variable?
Static Variables:
A static variable is associated with the class has only one copy per class but not for each object. An instance of a class does not have static variables.
Static variables can be accessed by static or instance methods
Memory is allocated when the class is loaded in context area at run time.
Non-Static Variables:
Non-static variables will have one copy each per object. Each instance of a class will have one copy of non-static variables.
Instance variables can be accessed only by the instance methods.
Instance variables are allocated at compile time.
24. What is the difference between general static variable and final static variable?
25. Which modifiers are applicable for local variables?
26. When the static variables are created?
·        It is a variable which belongs to the class and not to object(instance)
·        Static variables are initialized only once, at the start of the execution.
·        These variables will be initialized first, before the initialization of any instance variables
·        A single copy to be shared by all instances of the class
·        A static variable can be accessed directly by the class name and doesn’t need any object.

27. What are various memory locations of instance variables, local variables and static variables?
28. Is it possible over load a main method?
29. Is it possible to override static methods?
30. What is native keyword? Where it is applicable?
Native methods allow you to use code from other languages such as C or C++ in your java code. You use them when java doesn't provide the functionality that you need.
31. What is the main advantage of native keyword?
It marks a method, that it will be implemented in other languages, not in Java. It works together with JNI (Java Native Interface).
Native methods were used in the past to write performance critical sections but with Java getting faster this is now less common. Native methods are currently needed when
·        You need to call a library from Java that is written in other language.
·        You need to access system or hardware resources that are only reachable from the other language (typically C). Actually, many system functions that interact with real computer (disk and network IO, for instance) can only do this because they call native code.

32. If we are using native modifier how we can maintain platform independent nature?
33. How we can declare a native method?
private native String getLine(String prompt);
·        Notice that the declarations for native methods are almost identical to the declarations for regular, non-native Java methods.
·        There are two differences. Native methods must have the native keyword. The native keyword informs the Java compiler that the implementation for this method is provided in another language.
·        Also, the native method declaration is terminated with a semicolon, the statement terminator symbol. because there are no implementations for native methods in the Java class file.

34. Is abstract method can contain body?
35. What is synchronized keyword where we can apply?
36. What are advantages & disadvantages of synchronized keyword?
37. Which modifiers are the most dangerous in java?
38. What is serialization & Explain how its process?
39. What is de serialization?
40. By using which classes we can achieve serialization & de serialization?
41. What is serializable interface & explain its methods?
42. What is marker interface & give an example
43. Without having any method in serializable interface, how we can get serializable ability for our object?
44. What is the purpose of transient keyword & explain its advantages?
45. Is it possible to serialize every java object?
46. Is it possible to declare a method or a class with transient?
47. If we declare static variable with transient is there any impact?
48. What is the impact of declaring final variable a transient?
49. What is volatile variable?
50. Is it possible to declare a class or method with volatile?
51. What is the advantage & disadvantages of volatile modifier?