• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract class example

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I am trying to do some basic coding for using an abstract class but I am having some trouble putting my imagination into coding. Can somebody please help me complete this code. Basically, I want to create an array of abstract cars and convert each abstract one into a hondacar and set the instance variable carname.
Thanks in advance for your precious time and help.
Abstract class

HondaCar class

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting code.
Just tell me my friend how are you planing to give a name to object car if that object do not have any member to do that.

Dejan
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how are you planing to give a name to object car if that object do not have any member to do that.


I know that car object has no name instance variable....but I am trying to convert the abstract car into a HondaCar object and then set the instance variable "carname" of HondaCar class.

I am not sure how perfect the example is ...but am trying to learn how I can use an abstract class, the new for-each construct and also casting. Please correct if I am using an incorrect example in the first place
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For each abstract car,I want to
Step1: convert abstractcar into HondaCar

Not sure you can do this, as you are trying to use Car objects as a more specific sublcass, HondaCar. It would be kind of like me having an array of generic Dog and trying to put them into SiberianHusky arrays. The husky will have more specific things (pull sled, be really big and furry) that a generic Dog won't. Now, you could take a subclass object and put it into a superclass array, since you can guarentee that a HondaCar is a Car. If I am wrong on this, someone please let me know.

Step2: setCarname() from types[] array using for-each construct

Just a basic method call: carTypes[n].setCarname("KITT"); Set up an iterator so you can increment n along with your loop and it should work.

Step3: put each hondacar object to the return array

This is simply assigning each element of the abstractcar array to an element in the HondaCar array.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by an "abstract car". Your code doesn't create five objects of Car type, it doesn't create any in fact. Instead you have five String objects. And I don't see that you have any way to convert a String into a Car.

As you know, you can't create a Car object directly, because Car is an abstract class. You can create objects of any of its non-abstract subclasses, though. (This is what it means for Car to be abstract, right?) However you only have one such subclass, namely HondaCar. So that limits your choice; to create an object of Car type, you must create a HondaCar object.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...Esther this how your HondaCar should look like. Although its no use doing it this way but probavly it solves your purpose of understandig abstract class. I dont know if I could do what you wanted, as i did not use for each loop.
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for all the thoughts and especially for the message posted by Neha Daga. I really appreciate your help and time.
reply
    Bookmark Topic Watch Topic
  • New Topic