Translate

Thursday, December 27, 2012

Core Java Interview Questions part-4


Q) Overloading & Overriding?
Overloading (Compile time polymorphism)
 Define two or more methods within the same class (or) subclass that share the same name and their number of parameter, order of parameter & return type are different then the methods are said to be overloaded.
· Overloaded methods do not have any restrictions on what return type of Method (Return type are different) (or) exceptions can be thrown. That is something to worry about with overriding.
· Overloading is used while implementing several methods that implement similar behavior but for different data types.

Overriding (Runtime polymorphism)
When a method in a subclass has the same name, return type & parameters as the method in the super class then the method in the subclass is override the method in the super class.

à The access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method.

· If the superclass method is public, the overriding method must be public.
· If the superclass method is protected, the overriding method may be protected or public.
· If the superclass method is package, the overriding method may be packagage, protected, or public.
· If the superclass methods is private, it is not inherited and overriding is not an issue.
· Methods declared as final cannot be overridden.

à The throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including its subclasses.
  
à Only member method can be overriden, not member variable     

            class Parent{
    int i = 0;
    void amethod(){
                System.out.println("in Parent");
                }
             }         
class Child extends Parent{
    int i = 10;
    void amethod(){
                System.out.println("in Child");
    }
}          
class Test{
public static void main(String[] args){
Parent p = new Child();
Child  c = new Child();
System.out.print("i="+p.i+" ");
p.amethod ();
System.out.print("i="+c.i+" ");
c.amethod();
}
}

O/p: - i=0 in Child     i=10 in Child

Q) Final variable
            Once to declare a variable as final it cannot occupy memory per instance basis.

Q) Static block
            Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to  the main method the static block will execute.

Q) Static variable & Static method
Static variables & 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. It may not access the instance variables of that class, only its static variables. Further it may not invoke instance (non-static) methods of that class unless it provides them with some object.

à When a member is declared a static it can be accessed before any object of its class are created.
à Instance variables declared as static are essentially global variables.
à If you do not specify an initial value to an instance & Static variable a default value will be assigned   automatically.
à Methods declared as static have some restrictions they can access only static data, they can only call other            static data, they cannot refer this or super.
à Static methods cant be overriden to non-static methods.
à Static methods is called by the static methods only, an ordinary method can call the static methods, but static methods cannot call ordinary methods.
à Static methods are implicitly "final", because overriding is only done based on the type of the objects
à They cannot refer “this” are “super” in any way.

Q) Class variable & Instance variable  & Instance methods  & class methods
Instance variable à variables defined inside a class are called instance variables with multiple instance of class, each instance has a variable stored in separate memory location.

Class variables à you want a variable to be common to all classes then we crate class variables. To create a class variable put the “static” keyword before the variable name.

Class methods à we create class methods to allow us to call a method without creating instance of the class. To declare a class method use the “static” key word .

Instance methods à we define a method in a class, in order to use that methods we need to first create objects of the class. 

No comments: