DISHA NIRDESHAN

Enlightens your career

Tuesday, June 28, 2011

Questions on Inheritance & polymorphism

QUESTIONS ON INHERITANCE :

 

Why is multiple inheritance not allowed in java?

The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).

In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.

Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.

 

Q: How can you do multiple inheritance in Java      

A: To achieve multiple Inheritances in java we must use Interface. We can create a class by extending one class or we can implement any number of interfaces for it. But the restriction is we have to implement / describe every method existing in the interfaces.

Q: Why java does not support inheritance of multiple superclasses?what is achieved by inheritance?

A: Handling the two classes always be a problem.Why we need to extends two classes at a time.If that required use two extends statements.Like this...

Class A extends B

{

......

}

class c extends A

{

}

problem cleared...

 

Q: How and when to prevent inheritance?

A:

By a class declared as final then that class cannot be inherited when preventing the inheritance: suppose if your class not required to be reused then only you can declare final.

 

Q: What are the types of Inheritance?

A:

Types of Inheritance supported by Java

Ø Single Inheritance &

Ø Multi level Inheritance.

    

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).

 



 Questions on Polymorphism:


Q:What is polymorphism?

A:

Polymorphism means any forms. The word poly means more than one i.e many morphism means in different form, thus polymorphism comes to a conclusion with a meaning called in many forms.

Polymorphism can be implemented in the Java language in the form of multiple methods having the same name. Java code uses a late-binding technique to support polymorphism; the method to be invoked is decided at runtime.

 

Q:What is Method Overloading ?

A:

In a class, the concept of method overloading does not allow the external user to be aware about the internal processing of the system

ü  It just allows to user to use the different implementations of same name collected together and react appropriately to the supplied parameters to get the desired output.

ü  Method Overloading allows the user to achieve the compile time polymorphism.

ü  Overloaded methods are always the part of the same class. These  methods have the same name, but they may take different input parameters.

ü  The arguments passed to a overloaded method may differ in type or in number, or both. 

ü   Overloaded methods may have the same or different return types.

The important points to note:

ü  A difference in return type only is not sufficient to constitute an overload and is illegal.

ü  You should restrict your use of overloaded method names to situations where the methods really are performing the same basic function with different data.

ü  The language treats methods with overloaded names as totally different methods and as such they can have different return types. However, since overloaded methods perform the same job with different data sets, they should produce same return type normally. There is one particular condition, however, under which it is sensible to define different return types. This is the situation where the return type is derived from the argument type and is exactly parallel with the arithmetic operators.

ü  Overloaded methods may call one another simply by providing a normal method call with an appropriately formed argument list. 

 

Q:What is Method Overriding?

A:

 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 in the superclass will be hidden. If you wish to access the superclass' version of an overridden method, you can do so by using 'super'. 

 

In order for any particular method to override another correctly:

 

  • The return type, method name, type and order of arguments must be identical to those of a method in a parent class.
  • The accessibility must not be more restrictive than original method.

 The method must not throw checked exceptions of classes that are

 

Q:What is runtime polymorphism in Java?

A: The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

Let's take a look at the following example:

class Bird {
  void whoAmI() {
    System.out.println("I am a generic Bird.");
  }
}
class Pigeon extends Bird {
  void whoAmI() {
    System.out.println("I am a pigeon.");
  }
}
class Parrot extends Bird {
  void whoAmI() {
    System.out.println("I am a Parrot.");
  }
}
class Crow extends Bird {
  void whoAmI() {
    System.out.println("I am a Crow.");
  }
}

class RuntimePolymorphismDemo {

  public static void main(String[] args) {
    Bird ref1 = new Bird();
    Bird ref2 = new Pigeon();
    Bird ref3 = new Parrot();
    Bird ref4 = new Crow();
    ref1.whoAmI();
    ref2.whoAmI();
    ref3.whoAmI();
    ref4.whoAmI();
  }
}

The output is

I am a generic Bird.
I am a Pigeon..
I am a Parrot.
I am a Crow.

Q: What is compile-time Polymorphism ?

A:

Overloading is called compile-time polymorphism because the compiler must decide how to select which method to run, based on the datatypes of the arguments.  For example, if the compiler were to compile the statement

        Address a = Address.get("www.Dishaeducation.com");

it could see that the argument was a String literal, and
generate code that called method #1.

public class Address {
        public static Address get(String a) { ... }  // #1
        public static Address get(byte [] b) { ... } // #2
    }

 

0 comments :