Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object casting program compilation error

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith thank you very much.After i got implement run() method to ApBase class that error is gone .Thanks
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic