Polymorphism in Java

Polymorphism in Java

1.What is method signature ?

Ans). Method signature represents the method name along with method parmeters.

2.What is method overloading ?

Ans). Writing two or more methods in the same class in such a way that each mehtod has same name
but with different method signatures – is called method overloading.

3. What is method overriding ?

Ans). Writing two or more methods in super and sub classes such that the methods have same name
and same signature is called method overriding.

4.What is the difference between method overloading and method overriding ?

A:MethodOverloading Method Overriding
Writing two or more methods with the same name but with different signatures is called method
overloading. Writing two or more methods with the same name and same signatures is called method
overriding. Method overloading is done in the same class. Method overriding is done in super and sub
classes. In method overloading, method return type can be same or different. In method overriding
method return type should also be same. JVM decides which method is called depending on
the difference in the method signatures. JVM decides which method is called depending on
the data type (class) of the object used to call the method. Method overloading is done when the
programmer wants to extend the already available features. Method overriding is done when the
programmer wants to provide a different implementation(body) for the same feature. Method
overloading is code refinement. Same method is refined to perform a different task.
Method overriding is code replacement. The sub class method overrides(replaces) the super class
method.

5. Can you override private methods ?

Ans). No, private methods are not available in the sub classes, so they cannot be overriden.

6.Can we take private methods and final methods as same ?

Ans). Yes. The Java compiler assigns the value for the private methods at the time of compilation.
Also private methods can not be modified at run time. This is the same cases with final methods also.
Neither the private methods nor the final methods can be overriden . So, private methods can be taken
as final methods.

7. What is coercion ?

Ans). Coercion is the automatic conversion between different data types done by the compiler.

8. What is conversion ?

Ans). Conversion is an explicit change in the data type specified by the operator.

9.What is final ?

Ans). ‘final’ keyword is used in two ways:
It is used to declare constants as:
Final double PI = 3.14159; // PI is constant
It is used to prevent inheritance as:
Final class A // sub class to A cannot be created.

10.What is the difference between dynamic polymorphism and static polymorphism ?

Ans). Dynamic polymorphism is the polymorphism existed at runtime. Here, Java compiler does not
understand which method is called at compilation time. Only JVM decides which method is called at
runtime. Method overloading and method overriding using instance methods are the examples for
dynamic polymorphism. Static polymorphism is the polymorphism exhibited at compile time. Here,
Java compiler knows which method is called. Method overloading and method overriding using static
methods; method overriding using private or final methods are examples for static polymorphism.

11.What is difference between primitive data types and advanced data types ?

Ans). Primitive data types represent single values. Advanced data types represent a group of values.
Also methods are not available to handle the primitive data types. In case of advanced data types,
methods are available to perform various operations.

12. What is implicit casting ?

Ans). Automatic casting done by the Java compiler internally is called implicit casting . Implicit
casting is done to converty a lower data type into a higher data type.

13.What is explicit casting ?

Ans). The cating done by the programmer is called explicit cating. Explicit casting is compulsory
while converting from a higher data type to a lower data type.

14. What is generalization and specialization ?

Ans). Generalization ia a phenomenon wher a sub class is prompted to a super class, and hence
becomes more general. Generalization needs widening or up-casting. Specialization is phenomenon
where a super class is narrowed down to a sub class. Specialization needs narrowing or down-casting.

15.What is widening and narrowing ?

Ans). Converting lower data type into a higher data type is called widening and converting a higher
data type into a lower type is called narrowing. Widening is safe and hence even if the programmer
does not use cast operator, the Java compiler does not flag any error. Narrowing is unsafe and hence
the programmer should explicitly use cast operator in narrowing.

16.Which method is used in cloning ?

Ans). clone( ) method of Object class is used in cloning.

17.What do you call the interface without any members ?

Ans). An interface without any members is called marking interface or tagging interface. It marks the
class objects for a special purpose. For example, Clonable(java.lang) and Serializable(java.io) are two
marking interfaces. Clonable interface indicates that a particular class objects are cloneable while
Serializable interface indicates that a particular class objects are serializable.

0 comments:

Post a Comment