Translate

Thursday, December 27, 2012

Core Java Interview Question Part-6


Q) When we use Abstract class?
A) Let us take the behaviour of animals, animals are capable of doing different things like flying, digging,
Walking. But these are some common operations performed by all animals, but in a different way as well. When an operation is performed in a different way it is a good candidate for an abstract method.

Public Abstarctclass Animal{
  Public void eat(food food)  {
  }
  public void sleep(int hours) {
  }
  public abstract void makeNoise()
}

public Dog extends Animal
{
   public void makeNoise()  {
            System.out.println(“Bark! Bark”);
    }
}

public Cow extends Animal
{
   public void makeNoise() {
            System.out.println(“moo! moo”);
    }
}
 
Q) Interface
            Interface is similar to class but they lack instance variable, their methods are declared with out any body. Interfaces are designed to support dynamic method resolution at run time. All methods in interface are implicitly
abstract, even if the abstract modifier is omitted. Interface methods have no implementation;

Interfaces are useful for?
a) Declaring methods that one or more classes are expected to implement
b) Capturing similarities between unrelated classes without forcing a class relationship.
c) Determining an object's programming interface without revealing the actual body of the class.

Why Interfaces?
“ one interface multiple methods “ signifies the polymorphism concept.

à Interface has visibility public.
à Interface can be extended & implemented.
à An interface body may contain constant declarations, abstract method declarations, inner classes and inner interfaces.
à All methods of an interface are implicitly Abstract, Public, even if the public modifier is omitted.
à An interface methods cannot be declared protected, private, strictfp, native or synchronized.
à All Variables are implicitly final, public, static fields.
à A compile time error occurs if an interface has a simple name the same as any of it's enclosing classes or interfaces.
à An Interface can only declare constants and instance methods, but cannot implement default behavior.
à top-level interfaces may only be declared public, inner interfaces may be declared private and protected but only if they are defined in a class.

à A class can only extend one other class.
à A class may implements more than one interface.
à Interface can extend more than one interface.

Interface A
{
            final static float pi = 3.14f;
}

class B implements A
{
            public float compute(float x, float y) {
                        return(x*y);
            }
}

class test{
public static void main(String args[])
{
            A a = new B();
            a.compute();
}
}
           
Q) Diff Interface & Abstract Class?
à A.C may have some executable methods and methods left unimplemented. Interface contains no implementation code.
à An A.C can have nonabstract methods. All methods of an Interface are abstract.
à An A.C can have instance variables. An Interface cannot.
à An A.C must have subclasses whereas interface can't have subclasses
à An A.C can define constructor. An Interface cannot.
à An A.C can have any visibility: public, private, protected. An Interface visibility must be public (or) none.
à An A.C can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior.

Q) What is the difference between Interface and class?
à A class has instance variable and an Interface has no instance variables.
à Objects can be created for classes where as objects cannot be created for interfaces.
à All methods defined inside class are concrete. Methods declared inside interface are without any body.

Q) What is the difference between Abstract class and Class?
à Classes are fully defined. Abstract classes are not fully defined (incomplete class)
à Objects can be created for classes; there can be no objects of an abstract class.

Q) What are some alternatives to inheritance?
A) Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass). 

No comments: