aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Practice exam 1 K&B q 58 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Practice exam 1 K&B q 58" Watch "Practice exam 1 K&B q 58" New topic
Author

Practice exam 1 K&B q 58

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 253

public class Hose<E extends Hose>
{
E innerE;

public E get(E e,Hose<E>e2)
{
return e.getE(); //compiler says found Hose
}

E getE()
{
return innerE;
}
}

I know for compiler get() methods return type can only be E nothing else

Please explain clearly ,


Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
Mahtab Alam wrote:public class Hose<E extends Hose>
{
E innerE;

public E get(E e,Hose<E>e2)
{
return e.getE(); //compiler says found Hose
}

E getE()
{
return innerE;
}
}

I know for compiler get() methods return type can only be E nothing else

Please explain clearly ,


I guess it's because when get(E e,Hose<E>e2) is called, e must be instantialized as a specific object corresponding to specific class.
If we want to return E, we have to add a cast. (change return e.getE() to return (E) e.getE() )
On the other hand, if e is not involved, it always works. (Change return e.getE() to return getE() )
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
E e is an element, while Hose<E> e2 is a Host class of E with a method getE() that returns E. That is my take.


"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
The trick is what innerE is when Hose is used as a raw type. Hose h = new Hose() is different than Hose<Hose> e = new Hose<Hose>();

Look at this example:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Practice exam 1 K&B q 58