Sunday, April 10, 2011

SCJP Exam Watch 10 - Methods return an abstract class or interface

Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method - for example:

public abstract class Animal {}
public class Bear extends Animal {}
public class Test {
    public Animal go () {
        return new Bear();    // OK, Bear "is-a" Animal
    }
}

This code will compile, the return value is a subtype.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

No comments:

Post a Comment

Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.