• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Object casting program compilation error

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith thank you very much.After i got implement run() method to ApBase class that error is gone .Thanks
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic