• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialization and Constructors  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Electronics class, from which a Computer, CellPhone, and Camera class are extended. The latter three classes will all have a set of similar characteristics and then each subclass will have its own properties. Is it possible to create an object (i.e. Computer comp1 = new Electronics() ) where Electronics has the parameters manufacturer, brand, power consumption...etc... Electronics class is my main class in this program. When I tried to do this, it flagged the code saying, "Electronics cannot be converted to Computer".

Without doing it this way, I have redundant code within each subclass that I would prefer to discard.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to do this, do I need to create a new main class and simply have the desired Electronics constructor within the Electronics class thereby eliminating said constructor from the main class. --- This does not work either. Just trying to express my troubleshooting...what I'm trying to do is a reverse dynamic binding.



 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you right you have a class "Electronics"
and three subclasses: Computer, CellPhone, Camera

Following oriented object principles the "Electronics" class should be Abstract. you should never be able to create an "Electronics". Just a Camera (which happens to be Electronics as well). Line 5 of your code is invalid because of that.
All of the 'shared' code should be in the superclass "Electronics"
All of the custom stuff for each subtype of Electronics object should be in the appropriate subclass.

So Electronics I presume has manufacturer, brand etc
Computer would extend that and define "processor" and "ram".

Note that this is valid:

But this is not
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Stefan,

Yes, you understood correctly. With line 5 of my code being invalid, this negates what it is I am trying to accomplish, which is to have a single constructor with those similar properties as parameters.
I want to avoid using this redundant code for each subclass:
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now I understand your question.

Each object has to have its own constructor. Thats a given. A class ALWAYS has a constructor.
The first thing a constructor has to do is call the constructor of its parent class.
That is implicitly the no-args constructor... but it doesn't have to be.


Does that get rid of your 'redundant' code?
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you are doing, but it still seems redundant to me. Maybe its just not permitted in Java. I know each class as a default constructor, but I was thinking I could avoid using the same overloaded constructor in each class. There must be some way of avoiding the redundancy, no?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm following this. Sounds like you need an EquipmentFactory object with createEquipment() methods with appropriate overloading that is responsible for invoking the appropriate object's constructor and returning it.

Edit: Maybe that should have been 'Electronics', not 'Equipment'.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well right now, I have a main class Equipment after which I have the abstract class Electronics and then extended subclasses of Electronics.

All I'm trying to do is to refrain from having the same code in each subclass and I thought that since these properties were consistent across each subclass they would be encoded within the Electronic class, since each electronic has a brand, manufacturer, etc. It seems like a fairly simple task yet I can't manage to figure it out....
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:but it still seems redundant to me.

What exactly do you find redundant, that the different constructors have almost identical parameter lists?
Maybe its just not permitted in Java.

What do mean "it" here? Not quite clear what "it" is. What were you thinking of doing instead? (Edit) => Did you mean avoiding redundancy?
I was thinking I could avoid using the same overloaded constructor in each class. There must be some way of avoiding the redundancy, no?

Not really sure, but it seems like you might be thinking that constructors are inherited. They are not. Every class must provide at least one constructor. If no explicit constructor is defined, Java will provide a default no-argument constructor for you. So there's really no escaping having some "duplication" in the constructor signatures throughout a class hierarchy such as what you are trying to define.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you find redundant, that the different constructors have almost identical parameter lists?

The overloaded constructors for each subclass is identical and I don't like it. I want to avoid that redundant code. I would prefer this code to only be printed once in my code and I thought I could do that by placing it within the Electronics class, but I am unable to do that since an abstract class cannot include a constructor of any type.

What do mean "it" here?

The reverse dynamic binding that I was talking about to explain what I wanted to do. I was hoping to create a subclass object (computer, camera..etc) with the Electronic constructor (new Electronic(String manufacture, String brand...)).

Not really sure, but it seems like you might be thinking that constructors are inherited. They are not. Every class must provide at least one constructor. If no explicit constructor is defined, Java will provide a default no-argument constructor for you. So there's really no escaping having some "duplication" in the constructor signatures throughout a class hierarchy such as what you are trying to define.


This is the closest answer I think to what I was trying to ask. I apologize for the confusion. I was told one time that if you start to see a lot of redundant code, you should look for an alternative way of accomplishing that task...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:I was told one time that if you start to see a lot of redundant code, you should look for an alternative way of accomplishing that task...

That's good advice. Unfortunately, there's a limit to what you can do with regard to constructors in Java. The one thing I can think of that might help is a copy constructor. This would require you to have a "model" object from which you can copy attributes.

The nice thing about this is that you can leverage inheritance. That is, even though the declared type in the constructor is Equipment, you can pass anything that is a subclass of Equipment as well. So you can copy Equipment attributes from another Equipment instance, or you can copy from a Phone when you construct a Camera, or vice versa. You can now even construct Equipment from a copy of a Camera or Phone because the Camera and Phone are both also instances of Equipment.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:
What exactly do you find redundant, that the different constructors have almost identical parameter lists?

The overloaded constructors for each subclass is identical and I don't like it.


But it's not identical.
Yes, the constructor signature is, but the core of the constructor is different.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not identical.
Yes, the constructor signature is, but the core of the constructor is different.


I had assumed that was the implication when I said that it was identical -- it being the constructor signature...since that is the redundant code, not the object constructors..
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu - That is probably the only way to implement what it is that I was trying to do. This is probably a better way than I was using actually because I can just use the constructors of each subclass (camera, computer..etc) to define my objects instead of using mutator methods. ;) You all have been really helpful.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu - That is probably the only way to implement what it is that I was trying to do. Also, I think I've got a much better understanding of what I can do with inheritance. This is probably a better way than I was using actually because I can just use the constructors of each subclass (camera, computer..etc) to define my objects instead of using mutator methods. ;) You all have been really helpful.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:
But it's not identical.
Yes, the constructor signature is, but the core of the constructor is different.


I had assumed that was the implication when I said that it was identical -- it being the constructor signature...since that is the redundant code, not the object constructors..


It's no more redundant than the fact that java.util.List defines a subList(int fromIndex, int toIndex) method which all its children (and there are lots) also have to implement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!