Inheritance: -
subclass inheriting a general super-class gains access to all instance variables and methods defined by superclass and also over-rides some of superclass methods as and when required.
Polymorphism: 2 types – 1) Method Polymorphism (method overloading) and 2) Object Polymorphism. 1) Method Polymorphism – two methods of a class with same name but different signature. 2) Object Polymorphism – using parent class’s reference object to point to any of the child’s object and calling the child’s method which is also, termed as the ‘Dynamic Method Dispatch’. Parent o1 = new Parent(); Parent o2 = new Child();
Interface: - named collection of method definitions without implementations, that defines a protocol of behavior for a class. - supports multiple inheritance, Ex. Class1 is adhering to an interface, and if Class2 wants to communicate with Class1, then Class2 needs to follow the same interface. In short, the interface defines a contract to the external consumer of the class. - An interface which does not have any methods defined in it is called a MarkerInterface. - Marker is nothing but like an indication, which means it says some intimation to the JVM or Container. For example, we want to store an object in the file then we have to intimate to the compiler that this object should be stored. This intimation can be done by implementing the interface java.io.Serializable. - All methods are by default public, static and final; and the implementing class must implement all the methods defined in the interface. - a reference can be made to the interface but not object i.e. can not be instantiated.
Abstract Class: - can not be instantiated, but can only be inherited, and also does not support multipleinheritance. - same as interface except that it can have at least one method with definition, provided other methods are defined as ‘abstract’ methods which should be implemented by the subclass.
- can be used as a marker but then we can not extend any other class, so it is always better use interface as a marker. -
When to use what depends on various reasons, one being choice of design? -
When we do not want any one to create object of our class we define the class as abstract.
-
When we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will.
-
One reason for using abstract classes is we can code common functionality and force our developer to use it. I can have a complete class but I can still mark the class as abstract. Developing by interface helps in object based communication.
-
The difference is that in interface all are public by default, but in abstract class u can have private and protected members.
-
There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.
Static/Non-Static Fields: -
the static members are shared across the instances of a class, whereas the non-static members will be a copy specific to each instance of the class.
-
Non-static method can not be referenced from a static context. Ex. Cannot access static instance variables from a non-static method.
-
Can static methods be overridden? --YES---"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the super-classes and super-interfaces of the class that would otherwise be accessible to code in the class". Static Method definitely can be overridden. The only constraint is that it does not support polymorphism. So it's not called "overriding", it's called "hiding". Overriding a method implies polymorphic behavior and, as you said, static methods do not exhibit polymorphic behavior. FFI please check this link, http://radio.javaranch.com/corey/2004/03/19/1079739453000.html
-
Garbage Collection: -
This is the process of automatically freeing objects that are no longer in use.
-
Even though garbage collector has a good algorithm and it runs in its own thread thus having a least impact on the application performance but still, it has some impact on the application performance. Checks periodically for objects to be released from memory and also the Java runtime calls the “finalize()” method before releasing an asset.