This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object  casting program compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object  casting program compilation error" Watch "Object  casting program compilation error" New topic
Author

Object casting program compilation error

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



Just now I started to learn object reference conversion and casting.Looks like I got it when I work out in paper.But I need to work it in computer.Its showing the error I need to make the ApBase and ApDerived classes as abstract.but the question given is like

class ApBase extends Objects implementsRunnable
class ApDerived extends ApBase implements Observer
//Following variables created from above classes
ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();
which of the following compile and run w/o error ?
A) Object obj = aBase;
Runnable rn obj;
B) Object obj = aBase;
Runnable rn = (Runnable)obj; ..........Answer
C) Object obj = aBase;
Observer ob = (Observer)aBase;
D) Object obj = aDer;
Observer ob2 = obj;

why do I need to make those classes abstract.If I make it abstract then abstract classes won�t instantiate like this

ApBase aBase = new ApBase();
ApDerived aDer = new ApDerived();

Please help me to execute the above program.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You're getting that error because the class definitions say they are going to implement the Runnable interface and the Observer interface, but there is no definition for the method in those interfaces.

An implements clause is a contract. The class definition is allowed to be treated as an instance of the interface, but it must give concrete implementations to all of the methods listed in the interface.
[ October 11, 2006: Message edited by: Keith Lynn ]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Keith thank you very much.After i got implement run() method to ApBase class that error is gone .Thanks
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,


for Runnable you have it already,
for Observer:
public void update(Observable o, Object arg) {
}


You need to import import java.util.Observer;
for Observer as well.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object casting program compilation error
 
Similar Threads
Casting
casting
help me
Casting class types
casting problem from bill brogden mock exam