OCJP(Oracle Certified Java Program)



Totel:22 Click: 1 2 3 4 5
scjp1.5 Interview Questions And Answers

Page 1

Have to remember only six rules for returning a value:

We can return null in a method that has an object reference return type.

public Button doStuff() {
return null;
}
</code>
An array is a perfectly legal return type.
<code>
public String [] go() {
return new String[] {"Fred", "Barney", "Wilma"};
}


In a method with a primitive return type, we can return any value or variable that can be implicitly converted to the declared return type.

public int foo() {
char c = 'c';
return c; // char is compatible with int
}


In a method with a primitive return type, We can return any value or variable that can be explicitly cast to the declared return type.

public int foo () {
float f = 32.5f;
return (int) f;
}


We must not return anything from a method with a void return type.

public void bar() {
return "this is it"; // Not legal!!
}


In a method with an object reference return type, We can return any object type that can be implicitly cast to the declared return type.

public Animal getAnimal() {
return new Horse(); // Assume Horse extends Animal
}
public Object getObject() {
int[] nums = {1,2,3};
return nums; // Return an int array, which is still an object
}
public interface Chewable { }
public class Gum implements Chewable { }
public class TestChewable {
// Method with an interface return type
public Chewable getChewable {
return new Gum(); // Return interface implementer
}
}

As we know that When a subclass wants to change the method implementation of an inherited method,
the subclass must define a method that matches the inherited version exactly.

An exact match means the arguments and return types must be identical.We know that overloaded methods can change the return type, but overriding methods cannot.

Overloading is not much more than name reuse. The overloaded method is a completely different method from any other method of the same name. 

So if we inherit a method but overload it in a subclass, we are not subject to the restrictions of overriding,overload a method, remember, we must change the argument list.

The following code shows an overloaded method :

public class Foo{
void go() { }
}
public class Bar extends Foo {
String go(int x) {
return null;
}
}

This objective covers two aspects of return types : 
  1. What we can declare as a return type, and
  2. What we can actually return as a value.

It all depends on whether we are overriding an inherited method or simply declaring a new method.We will cover a small bit of new ground, though, when we look at polymorphic return types and the rules for what is and is not legal to actually return.

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument lists, like the following examples :

class Foo {
Foo() { }
Foo(String s) { }
}


The preceding Foo class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember since there is already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor.


Goto Page:

1 2 3 4 5