Sunday, October 17, 2010

Basics of Polymorphism in JAvA

HI,
static Polymorphism : Stating more than one functionality with the same name,but with the different arguments in the same class is known as Static Polymorphism.

With the four functions present in a Java Program.The function which has to be executed is decided at the compile time itself is known as Static Polymorphism.

Static Polymorphism can be achieved through the function Overloading.

Disadvantages of static polymorphism:
JVM can go into an Ambiguous code.

Simple Example :
Class Ploy {
  void function1()
{
  S.o.p("Inside Function");
}
void function2(int x)
{
 s.o.p("Inside Function 2");
}
void function1(){
return;}

Note: return datatype of a functions is not applicable in java as a function overloading

What is Dynamic Polymorphism?
Some functions with same name and same arguments the function which has to be executed is
decided at run time is called Dynamic Polymorphism.

What are the other names of Dynamic Polymorphism?
Ans: Run time Polymorphism.
       late - binding.

How come we achieve the Dynamic Polymorphism and static polymorphism?
Dynamic Polymorphism can be achieved by the function Overloading.
Static polymorphism can be achieved by the function Overriding.

Note: Dynamic Polymorphism can be achieved only through the Inheritance


Question : Give an example where Static polymorphism leads to ambiguity code.
Ans:
         Class staticPolymorphism
{
      public void function1(int x, int y)
{
   S.o.p(" Function1 ");
}
 public void function2(int a, int b)
{
   S.o.p("Function 2");
}
public static void main(String[] args)
{
  staticPolymorphism sp = new StaticPolymorphism();
 sp.function1(5,6);  // leads to ambiguity
sp.function2(34,12);  // leads to ambiguity.
}
}

calling functions leads to Ambiguity because function1 and function2 have the same
function Signatures.

Remember : We cannot have two functions with same and same
 signature and same Priority in one class.

Question: What are the types of functions Present in the println method in java?

System.out.println();

one) can accept the object of a class(user defined);
two) object of string class.
three) object of array class.

What is the Difference between late binding and dynamic polymorphism in java?
Ans: Dynamic polymorphism : The function to be executed is decided at the run time,
        but note that by the time of compilation the function has a body.

        late binding : The body of the function itself is going to get associated with the function
        definition during run time.

Question : How to implement late binding in java?
Ans: we can implement it through the Interfaces in java.


Question : What are the types of structures in Java?
Structure Type 1  :  Fully Implemented Structure.
Structure Type 2 :  Fully Un-implemented Structures.
Structure Type 3:   Partially implemented/un implemented structure.


Structure Type 1 --------> Class.
Structure Type 2 --------> Interface.
Structure Type 3 --------> Abstract Class.


Note: Java Compiler will convert any valid structure into equivalent 
byte-code(.class) file.


Example of Interface:
---------------------------------------------
  interface Xyz
{
   public void functionX();
   public void functionY();
}
class Abc
{
  ------------
  --------------
}
-----------------------------------------------------
Note: All the Classes who are going to implement the interface should provide
 the bodies for the definitions inside the Interface.

very Very Important  Note:


We can define a reference for any .class file, but we can only create 
object only for the classes.

So from the above point we cannot create object for the Abstract class and Interface.

What is the difference between extends and implements keyword?
Ans : extends -> get the properties from another structure.
         implements - > provide bodies for function inside a structure.
extends ===== gettting.
implements ==== Giving.

Reason: we can extends only one class utmost, but we can implement any number of
 interfaces.

Note: if there are two definitions which looks same in two different interfaces.
Then the class which going to implements these interfaces can provide the body
for the definition is enough.

Important Note : we can assign the object of a class which implements the interface
 to the interface reference.
Example : XYZ x = new A();
interface XYZ
{
   public void funtion();
}
 class A implements XYZ
{
   public static void main(String args[])
{
    XYZ x = new A();
    x.funtion();
}
public void function()
{
   System.o.p("Providing the body for the Function in Interface");
}
}

Important points on INTERFACES:
1) All the variables we declare inside the interface are default "public static final"
2) All the Functions in interface are default public void.
3) we cannot use keyword "static"  before the functions definitions inside
     the interfaces.
4) static constants inside the interface should be initialized by the Programmer.


Example :
 interface Abc
{
  public static final int x = 40;
  int y = 30;//by default it is public static final.
  public void function();
  public static void function(); // ERROR: should not add keyword "static"
  int y ; // Error: should be initialized to some value.


Note: An Interface can extends any number of interfaces.


Note:An interface cannot implement another interface, because it should 
          provide the body, where interface cannot do that.


Note: 
1) we can typecast an interface reference into it's equivalent reference of a class.
  Example :  Interface x = new Class();
                    class c = (Class) x;
2) we can typecast an interface reference into it's equivalent reference of another
    Interface.
                    Interface1 i1 = (interface1) i2;
             where i2 is the reference of another Interface.













No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...