Showing posts with label Overriding. Show all posts
Showing posts with label Overriding. Show all posts

Wednesday, 4 January 2012

Sun Certified Java Programmer (SCJP) Questions

Which of the choices below can legally be inserted at the "insert code here" position in the following code?


class Parent
{


public void print(int i)


{
}


}


public class Child extends Parent
{


public static void main(String argv[])
{
}


// insert code here


}


Choices:

A. public void print(int i, byte b) throws Exception {}


B. public void print(int i, long i) throws Exception {}


C. public void print(long i) {}


D. public void print(int i) throws Exception {}


E. public int print(int i)


Correct choices:

A, B, and C


Explanation:

Friday, 30 December 2011

Polymorphism in Java

Polymorphism means "any forms." In object-oriented programming, it refers to the capability of objects to react differently to the same method. 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.

Overloaded methods are methods that have the same name, but different argument lists. Overriding, on the other hand, occurs when a subclass method has the same name, same return type, and same argument list as the superclass method.

Overloading


As we mentioned, it is mandatory for overloaded methods to have the same names but different argument lists. The arguments may differ