File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method inheritance 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 "Method inheritance" Watch "Method inheritance" New topic

Method inheritance

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
Given the following object hierarchy and code for the upgrade method:
+---- mypkg.BaseWidget
+---- TypeAWidget
// the following is a method in BaseWidget
1. public TypeAWidget upgrade( ){
2. TypeAWidget A = (TypeAWidget) this ;
3. return A ;
4. }
What will be the result of trying to compile and run a program containing the following statements?
5. BaseWidget B = new BaseWidget() ;
6. TypeAWidget A = B.upgrade() ;

A. The compiler will object to line 2.
B. A runtime ClassCastException will be generated in line 2.
C. After line 6 executes, the object referred to as A will in fact be a TypeAWidget.

Why is the answer B?
Explain me more clear .
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

The code attempts to 'downcast' the current type to a subtype. This is inherently dangerous, as it can work out in some cases, but guaranteed for all cases.

If the subtype adds methods to its supertype interface, for example, how can the supertype claim to support it? It has no understanding of extensions to itself.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
I didnot understand it .
please explain me with example clearly .
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From the diagram(?) class TypeAWidget is a subclass of class BaseWidget. So every TypeAWidget object is a BaseWidget object. It is not so that every BaseWidget object is a TypeAWidget object. Some might be, but not all.

Java lets you express "this object might be one of these objects" with a so-called downcast, and the compiler will accept the cast as a promise that ,at runtime, you will provide a TypeAWidget object. The compiler will only accept the promise if it looks feasible that the classes have such a relationship to each other - it could reject the cast if obviously no such relationship exists.

TypeAWidget A = (TypeAWidget)this is making exactly that promise in the upgrade method. But at runtime the this in method upgrade() is definitely only of type BaseWidget (see line //5), not of type TypeAWidget. The Java Virtual Machine detects your broken promise to provide a TypeAWidget object at runtime and complains very loudly with a ClassCastException.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Method inheritance
It's not a secret anymore!