• Post Reply Bookmark Topic Watch Topic
  • New Topic

How i create objects superclass-subclass  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mind is ''stuck'' on objects.I have a theoritic part that i cant explain ,i am confusing.
Problem : example i have a super class. I have 3 subclass .
Example Lets say i have a car (super class).I have 1 subclass(hon) ,1 subclass(nis),1 subclass(subar).On the car(super class) i have 1 method that give lets say a color to the car blue or red or white .Blue for hon,red for nis and white for subar.The subclass nis the ''car type '' lets say is professional.The subclass hon for normal car and the sublcass subar for sport.Also ,hon has 2doors ,nis has 4doors and subar 1 door and windows.

Question : How can i create car using objects?

What i am thinking and what am i doing in exercises :

First:I create ''fields'' example inside the subclass hon,nis,subar i create the doors i mean
Second:I create get and set methos. example and for set
Third:I create the empty and full constructor .example :empty or i can with this way and full constructor
Four:i create 3 arrays with objects.One for hox,one for nis and one for subar.I should put it on the main?on the superclass?on the subclass?I put it usually on superclass.
five:Here is my problem how to create the car.How to give ''prices'' i lost in this part.I have try this but i am ''losing'' it .

Hint:Basically four and five steps there I have problem.I should have 3arrays? One array that includes all this? How I create "car" in this case.Something more i neglect it was : Ofcourse I have fields ,constructors and in the superclass car
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:i create 3 arrays with objects.One for hox,one for nis and one for subar.

Ekte, you really have to learn to be accurate. Class names should start with a CAPITAL letter, and you really should give them proper names - ie, 'Honda', 'Nissan' and 'Subaru'; not 'hox', 'nis' and 'subar'.

I also think you're trying too much at once. Concentrate on ONE subtype, with ONE extra field, and try to create an object for it. Then, when - and ONLY when - you've got that working, add another...

There'll be plenty of time for Engines and arrays later on.

Winston
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
ekte spiriopoulos wrote:i create 3 arrays with objects.One for hox,one for nis and one for subar.

Ekte, you really have to learn to be accurate. Class names should start with a CAPITAL letter, and you really should give them proper names - ie, 'Honda', 'Nissan' and 'Subaru'; not 'hox', 'nis' and 'subar'.

I also think you're trying too much at once. Concentrate on ONE subtype, with ONE extra field, and try to create an object for it. Then, when - and ONLY when - you've got that working, add another...

There'll be plenty of time for Engines and arrays later on.

Winston
ok no problem you have right .I just see you my job .At least one then,how can i create windows ?
first thing is to write this on a class Honda : second in the full constructor after the methods get and set as i describe them up.(public int getwindows).Ok now the difficult part.First ,where i should make an array of car?i should create 3 arrays or 1 ? How i create a car and the windows for Honda?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First ,where i should make an array of car?

This depends on why you're making the array. And can you use List instead?

i should create 3 arrays or 1 ?

Need to know why you're creating the array. What is its use?

How i create a car and the windows for Honda?

From what you've said so far, I would think:

but I would have to see the code you've created. Can you post what you've done so far?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you writing no‑arguments constructors? All you achieve like that is permitting users to create objects without setting up their fields.
Campbell's rule of constructors:-
You must write one constructor for every class you write, but you should write as few constructors as possible.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!