Abstract class : A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Interface : In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Garbage collection : Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.
OOP : Object-oriented programming - There are three main principals of OOP which are called Polymorphism, Inheritance and Encapsulation.
Inheritance : Inheritance is the process by which one object acquires the properties of another object.
Polymorphism : The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".
Collection API : The Collection API is a set of classes and interfaces that support operation on collections of objects.
Collection classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Collection interfaces: Collection, Set, List and Map.
Transient : Transient variable can't be serialize
Synchronization : Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time.
Iterator : Iterator is an interface which is used to step through the elements of a Collection
Difference between an Abstract class and Interface
- Interfaces provide a form of multiple inheritance. A class can extend only one other class.
- Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
- A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
- Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
Similarities : Neither Abstract classes or Interface can be instantiated.
Can an Interface have an inner class? Yes.
Can we define private and protected modifiers for variables in interfaces? No
What is Externalizable? Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)
Only public and abstract modifiers are allowed for methods in interfaces.
What is a local, member and a class variable? Variables declared within a method are local variables. Variables declared within the class i.e not within any methods are member variables (global variables). Variables declared within the class i.e not within any methods and are defined as static are class variables
What does it mean that a method or field is static? Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). Thats how library methods like System.out.println() work. out is a static field in the java.lang.System class.
Checked and Unchecked Exceptions : All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you dont, compilation of program will fail.
Checked : Compile time exceptions (Exception).
Unchecked : Run time exceptions (RuntimeException & Error)
Field and method has an access level: private: accessible only in this class, (package): accessible only in this package, protected: accessible only in this package and in all subclasses of this class, public: accessible everywhere this class is available
If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method
Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned to again.
Methods java providing for Thread communications : Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll. These methods are defined for all Objects (not just Threads). The idea is that a method called by a thread may need to wait for some condition to be satisfied by another thread; in that case, it can call the wait method, which causes its thread to wait until another thread calls notify or notifyAll.
Difference between notify and notify All methods : A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify).
Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.
super() : In a class hierarchy, constructors are called in order of derivation, from
superclass to subclass. super() must be the first statement executed in a subclass
costructor, this order is the same wheather or not super() is used.
The constructors are called in order of derivation. If you think about it, it makes
sense that contructors are executed in order of derivation. Because a superclass has no
knowledge any subclass, any intialization it needs to perform is seperate from and possibly
prerequisite to any initialization performed by the subclass. Therefore it must be excuted
Method Overriding : In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass, then the method in the subclass is said to
override the method in the superclass.
When an overridden method is called from within a subclass, it will always refer to the
version of that method defined by the subclass. The version of the method defined by the
superclass will be hidden.
Method overriding occurs only when the names and the type signatures of the two methods
are identical. If they are not, then two methods are simply overloaded
- Interface - Collection : Ordered - no : Allows Duplicates - yes : Maps key to object - no
- Interface - List : Ordered - yes : Allows Duplicates - yes : Maps key to object - no
- Interface - Map : Ordered - no : Allows Duplicates - no : Maps key to object - yes
- Interface - Set : Ordered - no : Allows Duplicates - no : Maps key to object - no
- Interface - SortedMap : Ordered - yes : Allows Duplicates - no : Maps key to object - yes
- Interface - SortedSet : Ordered - yes : Allows Duplicates - no : Maps key to object - no
Final :Methods declared as final cannot be overridden.
Methods declare as final can sometimes provide a performance enhancement : The compiler is free to inline calls to them because it "knows" they will not be overridden by a subclass. When a small final method is called, often the java compiler can copy the bytecode for the subroutine directly inline with the compiled code of the calling method, thus eliminating the costly overhead associated with method call. Inline is only an option with final methods.
Normally, Java resolves calls to methods dynamically, at run time. This is called late binding. However, since final methods cannot be overridden, a call to one can be resolved at compile time. This is called early binding.
Using final to Prevent Inheritance. Declaring a class as final implicitly declares all of its methods as final, too.
It is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.
Variable declare as final is equivalent to named constant.